为什么我在这段代码中得到NullPointerException?

时间:2010-02-15 10:39:38

标签: google-app-engine jpa nullpointerexception

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.domainobject.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 {
    }
}

我在NullPointerException获得transaction.commit();。 因此,我无法将Department对象添加到Employee

交易:org.datanucleus.jpa.EntityTransactionImpl@1e4320e java.lang.NullPointerExceptionException ::空

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)

在提交交易之前,我已将其打印出来但仍然出现错误。

2 个答案:

答案 0 :(得分:1)

根据堆栈跟踪,问题出在“Google App Engine的DataNucleus插件”中。我狩猎了一下,似乎有一个关于这个特定问题的错误here

看起来已修复并稍后验证(2009年8月14日)。那么也许您可以尝试以后的版本,看看问题是否消失了?

答案 1 :(得分:0)

当您在NPE获得transaction.commit()时,transactionnull