Why do I get a NullPointerException in this code?
public void addEmp(String emp_name, String emp_sal, String did) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("transactions-optional");
EntityManager em = emf.createEntityManager();
try {
String s = did;
EntityTransaction transaction = em.getTransaction();
transaction.begin();
System.out.println("value of Department_name is::" + s);
Query q = em.createQuery("select from com.wissen.enterprisebysush.server.dom开发者_开发知识库ainobject.Department d");
Department dept = (Department) q.getResultList().get(0);
System.out.println("Name of Department is::" + dept.getDept_name());
Employee e = new Employee();
e.setEmp_name("Abvc");
e.setEmp_sal("5000");
e.setDepartment(dept);
em.persist(e);
System.out.println("Transaction: " + transaction);
transaction.commit();
em.close();
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println("Exception::" + e.getCause());
} finally {
}
}
I am getting a NullPointerException
at transaction.commit();
.
So I am unable to add Department
object in to Employee
Transaction: org.datanucleus.jpa.EntityTransactionImpl@1e4320e java.lang.NullPointerExceptionException::null
at com.google.appengine.api.datastore.KeyFactory.stringToKey(KeyFactory.java:181)
at org.datanucleus.store.appengine.DatastoreElementContainerStoreSpecialization.extractElementKey(DatastoreElementContainerStoreSpecialization.java:170)
at org.datanucleus.store.appengine.DatastoreAbstractCollectionStoreSpecialization.contains(DatastoreAbstractCollectionStoreSpecialization.java:57)
at org.datanucleus.store.mapped.scostore.AbstractCollectionStore.contains(AbstractCollectionStore.java:116)
at org.datanucleus.sco.backed.List.contains(List.java:455)
at org.datanucleus.state.RelationshipManager.processManyToOneBidirectionalRelation(RelationshipManager.java:690)
at org.datanucleus.state.RelationshipManager.process(RelationshipManager.java:294)
at org.datanucleus.state.JDOStateManagerImpl.processManagedRelations(JDOStateManagerImpl.java:2377)
at org.datanucleus.ObjectManagerImpl.performManagedRelationships(ObjectManagerImpl.java:2706)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:2744)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:2886)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:348)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:235)
at org.datanucleus.jpa.EntityTransactionImpl.commit(EntityTransactionImpl.java:104)
at com.wissen.enterprisebysush.server.GreetingServiceImpl.addEmp(GreetingServiceImpl.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:527)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:166)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:306)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
at org.mortbay.jetty.Server.handle(Server.java:313)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:844)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:644)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)
Before commiting the transaction I have printed it still getting an error .
The problem is inside "DataNucleus plugin for Google App Engine" according to the stacktrace. I hunted around a bit, and there seems to be a bug filed about this particular problem here.
It looks like it was fixed and later verified (Aug 14, 2009). So maybe you could try a later release and see if the problem goes away?
When you get an NPE
at transaction.commit()
, then transaction
is null
.
精彩评论