I'm using Cloud Endpoints to retrieve data from appengine to android. Using the generated library I retrieve a list of objects. This appears to work fine, and if I log the object I see the json. However if I use a getX() on the object I get a NPE. Java GAE 1.8.7. Doing this in an AsycTask in activity.
我正在使用Cloud Endpoints从appengine检索数据到android。使用生成的库我检索对象列表。这似乎工作正常,如果我记录对象,我看到了json。但是如果我在对象上使用getX(),我会得到一个NPE。 Java GAE 1.8.7。在AsycTask活动中执行此操作。
Builder builder = new Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(), null);
MyObjectOperations service = builder.build().myobject();
MyObjectCollection tmp = null;
GetMyObjects it = service.getMyObjects();
tmp = it.execute();
listOfMyObjects = tmp.getItems();
MyObject test=listOfMyObjects.get(0);
test.getMyField(); //NPE here - on every field
What could cause this?? The listOfMyObjects is the correct size. I have used this client lib before and it worked. Now it is an updated version. Any help would be great!
有什么可能导致这个? listOfMyObjects的大小正确。我之前使用过这个客户端库并且它有效。现在它是一个更新版本。任何帮助都会很棒!
1 个解决方案
#1
1
If your listOfMyObjects
is not an instance of ArrayList<>
or some other Iterable
, the get(0)
will return null, as the list isn't indexed. So, even if the list has n items, it doesn't have a specific nth item, as they're not ordered.
如果listOfMyObjects不是ArrayList <>或其他Iterable的实例,则get(0)将返回null,因为列表未编入索引。因此,即使列表中有n个项目,它也没有特定的第n个项目,因为它们没有被订购。
Create an ArrayList
like this:
像这样创建一个ArrayList:
listOfMyObjects = new ArrayList<myObject>(tmp.getItems());
Then get(0)
should work.
然后得到(0)应该工作。
#1
1
If your listOfMyObjects
is not an instance of ArrayList<>
or some other Iterable
, the get(0)
will return null, as the list isn't indexed. So, even if the list has n items, it doesn't have a specific nth item, as they're not ordered.
如果listOfMyObjects不是ArrayList <>或其他Iterable的实例,则get(0)将返回null,因为列表未编入索引。因此,即使列表中有n个项目,它也没有特定的第n个项目,因为它们没有被订购。
Create an ArrayList
like this:
像这样创建一个ArrayList:
listOfMyObjects = new ArrayList<myObject>(tmp.getItems());
Then get(0)
should work.
然后得到(0)应该工作。