我想弄清楚为什么这段代码会抛出NPE:
@RequestMapping(value = "/share/{id}/store/{typeId}", method = RequestMethod.PUT)
public ResponseEntity<Share> updateShareStore(@PathVariable final long id,
@PathVariable final String typeId) {
EntityManagerFactory emf = EMF.get();
EntityManager em = emf.createEntityManager();
Share share = null;
logger.debug(User.class.getName());
try {
share = em.find(Share.class, id);
Store store = em.find(Store.class, typeId);
share.getStores().add(store);
em.merge(share);
} finally {
em.close(); // NPE Here!
}
return new ResponseEntity<Share>(share,HttpStatus.OK);
}
错误:
java.lang.NullPointerException at com.google.appengine.datanucleus.StoreFieldManager.processPersistable(StoreFieldManager.java:453) at com.google.appengine.datanucleus.StoreFieldManager.getDatastoreObjectForCollection(StoreFieldManager.java:987) at com.google.appengine.datanucleus.StoreFieldManager.storeRelations(StoreFieldManager.java:877) at com.google.appengine.datanucleus.DatastorePersistenceHandler.updateObject(DatastorePersistenceHandler.java:426) at org.datanucleus.state.JDOStateManager.flush(JDOStateManager.java:3832) at org.datanucleus.ObjectManagerImpl.flushInternalWithOrdering(ObjectManagerImpl.java:3884) at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3807) at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3747) at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:4137) at org.datanucleus.ObjectManagerImpl.transactionPreCommit(ObjectManagerImpl.java:428) at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:400) at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:288) at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1090) at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:193) at org.demo.resources.ShareController.updateShare(ShareController.java:83) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606)
可能是因为我使用em
有两种对象类型吗?但这不太可能。