本文介绍了在GAE更新实体时,遇到空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和GAE和面临的问题,同时更新实体从Android应用程序在GAE(后端为Android)(含子对象列表的对象)

情景是:

我已经有保存的数据源中的父对象。然后,我获取该对象,并添加子对象(新建),然后火的更新列表。我的实体是:

父对象

 公共类ParentObject {
    @Id
    @GeneratedValue(策略= GenerationType.IDENTITY)
    私有密钥的关键;
    私人字符串placeId;

    @OneToOne(级联= CascadeType.ALL)
    私人列表< ChildObject> childObjects;

    公钥getKey(){
        返回键;
    }
    公共无效setkey的(关键的关键){
        this.key =键;
    }
    公共字符串getPlaceId(){
        返回placeId;
    }
    公共无效setPlaceId(字符串placeId){
        this.placeId = placeId;
    }
    公开名单< ChildObject> getChildObject(){
        返回childObjects;
    }
    公共无效setChildObject(名单< ChildObject> childObjects){
        this.childObjects = childObjects;
    }
}
 

子对象

  @Entity
公共类ChildObject {
    @Id
    @GeneratedValue(策略= GenerationType.IDENTITY)
    私有密钥的关键;
    私人字符串USEREMAIL;
    公钥getKey(){
        返回键;
    }
    公共无效setkey的(关键的关键){
        this.key =键;
    }
    公共字符串getUserEmail(){
        返回USEREMAIL;
    }
    公共无效setUserEmail(字符串USEREMAIL){
        this.userEmail = USEREMAIL;
    }
}
 

和更新的AsyncTask code是

  @覆盖
    保护ParentObject doInBackground(ParentObject ... PARAMS){
        ParentObject parentObj = PARAMS [0];

        Parentobjectendpoint.Builder建设者=新Parentobjectendpoint.Builder(
                AndroidHttp.newCompatibleTransport(),新JacksonFactory()
                空值);

        建设者= CloudEndpointUtils.updateBuilder(制造商);

        Parentobjectendpoint端点= builder.build();

        ParentObject updatedObj;

        尝试 {
            updatedObj = endpoint.updateParentObject(parentObj).execute();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
            updatedObj = NULL;
        }

        返回updatedObj;
    }
 

而我得到的错误是

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

解决方案

确定我能够通过改变ParentObject的主键来解决这个问题。

  @Id
@GeneratedValue(策略= GenerationType.IDENTITY)
私人龙ID;
 

很多在类似这样的问题,研究帮助我这个

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)
解决方案

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

这篇关于在GAE更新实体时,遇到空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:27