Getting Null Pointer exception when updating a ent

2019-08-15 02:27发布

I'm new to android and gae and facing issue while updating a entity (An object containing a list of child objects) in GAE (backend for android) from android app

Scenario is:

I already have parent object saved in datasource. I then fetch this object and add list of child object (newly created) and then fire update. My entities are:

Parent Object

public class ParentObject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;
    private String placeId;

    @OneToOne(cascade = CascadeType.ALL)    
    private List<ChildObject> childObjects;

    public Key getKey() {
        return key;
    }
    public void setKey(Key key) {
        this.key = key;
    }
    public String getPlaceId() {
        return placeId;
    }
    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }
    public List<ChildObject> getChildObject() {
        return childObjects;
    }
    public void setChildObject(List<ChildObject> childObjects) {
        this.childObjects = childObjects;
    }
}

Child Object

@Entity
public class ChildObject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;
    private String userEmail;
    public Key getKey() {
        return key;
    }
    public void setKey(Key key) {
        this.key = key;
    }
    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
}

And update AsyncTask code is

    @Override
    protected ParentObject doInBackground(ParentObject... params) {
        ParentObject parentObj = params[0];

        Parentobjectendpoint.Builder builder = new Parentobjectendpoint.Builder(
                AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
                null);

        builder = CloudEndpointUtils.updateBuilder(builder);

        Parentobjectendpoint endpoint = builder.build();

        ParentObject updatedObj;

        try {
            updatedObj = endpoint.updateParentObject(parentObj).execute();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            updatedObj = null;
        }

        return updatedObj;
    }

The error which I'm getting is

com.google.api.server.spi.SystemService invokeServiceMethod: null
java.lang.NullPointerException
at com.google.appengine.api.datastore.Key.getAppId(Key.java:279)
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:50)
at com.google.appengine.api.datastore.EntityTranslator.convertToPb(EntityTranslator.java:48)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:178)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:155)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:180)
at com.google.appengine.api.datastore.Batcher$2.<init>(Batcher.java:317)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:317)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchPut(AsyncDatastoreServiceImpl.java:365)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:293)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:239)
at com.google.appengine.api.datastore.DatastoreServiceImpl.put(DatastoreServiceImpl.java:61)
at com.google.appengine.datanucleus.WrappedDatastoreService.put(WrappedDatastoreService.java:112)
at com.google.appengine.datanucleus.EntityUtils.putEntitiesIntoDatastore(EntityUtils.java:766)
at     com.google.appengine.datanucleus.DatastorePersistenceHandler.insertObjectsInternal(DatastorePersistenceHandler.java:314)
at     com.google.appengine.datanucleus.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:218)
at org.datanucleus.state.JDOStateManager.internalMakePersistent(JDOStateManager.java:2381)
at org.datanucleus.state.JDOStateManager.flush(JDOStateManager.java:3778)
at org.datanucleus.ObjectManagerImpl.flushInternalWithOrdering(ObjectManagerImpl.java:3888)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3811)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3751)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:4141)
at org.datanucleus.ObjectManagerImpl.transactionPreCommit(ObjectManagerImpl.java:428)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:398)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:287)
at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1090)
at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:193)
at com.sandeepapplabs.dms.VehicleEndpoint.updateVehicle(VehicleEndpoint.java:130)
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:45)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:160)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:118)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:438)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:445)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:220)
at     com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:309)
at     com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:301)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:442)
at java.lang.Thread.run(Thread.java:724)

1条回答
▲ chillily
2楼-- · 2019-08-15 02:38

OK I was able to solve the issue by changing primary key of ParentObject to

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

A lot of research at questions similar to this helped me out with this

查看更多
登录 后发表回答