[英]Google cloud endpoints method always causes NullPointerException, why?
I am developing an android app which used google endpoints as a backend. 我正在开发一个使用谷歌端点作为后端的Android应用程序。 For some reason one of the endpoints methods only ever causes a
java.lang.NullPointerException
when called from the android client but not when called from the api explorer. 出于某种原因,其中一个端点方法只在从android客户端调用时才会导致
java.lang.NullPointerException
,但在从api资源管理器调用时则不会。
Here is the endpoints method: 这是端点方法:
@ApiMethod(name = "getFriends", path = "friends", httpMethod = ApiMethod.HttpMethod.GET)
public List<Profile> getFriends(@Named("userId") String userId) {
Profile profile = ofy().load().key(Key.create(Profile.class, userId)).now();
List<String> friendIds = profile.getFriendIds(); // NullPointerException on this line
List<Profile> friends = new ArrayList<>();
if (friendIds != null) {
for (String friendId : friendIds) {
Profile friend = ofy().load().key(Key.create(Profile.class, friendId)).now();
friends.add(friend);
}
}
return friends;
}
Here is the profile entity getFriendIds() method: 这是配置文件实体getFriendIds()方法:
public List<String> getFriendIds() {
return friendIds != null ? ImmutableList.copyOf(friendIds) : null;
}
In debug mode and in the API explorer, everything works as expected. 在调试模式和API资源管理器中,一切都按预期工作。 In this example I created 2 users, one with
id = 4567
and the other id = 9876
. 在这个例子中,我创建了2个用户,一个
id = 4567
,另一个id = 9876
。 Here is what happened when I ran the method to get the friends: 以下是我运行方法获取朋友时发生的事情:
It works as expected and caused no errors when called by api explorer. 它按预期工作,并且在api explorer调用时不会产生任何错误。
I then tried it when the user has no friends: 然后,当用户没有朋友时,我尝试了它:
It works as expected and caused no errors when called by api explorer. 它按预期工作,并且在api explorer调用时不会产生任何错误。
In my android client I call this exact method but the method always causes a java.lang.NullPointerException
. 在我的Android客户端中,我调用这个确切的方法,但该方法总是会导致
java.lang.NullPointerException
。
I call it from an async task like so: 我从异步任务中调用它,如下所示:
return mApi.getFriends(mUserId).execute().getItems();
Here is the stacktrace for the client side: 这是客户端的堆栈跟踪 :
07-12 14:09:57.934 9777-9830/com.hb.birthpay W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
07-12 14:09:57.934 9777-9830/com.hb.birthpay W/System.err: {
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "code": 503,
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "errors": [
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: {
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "domain": "global",
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "message": "java.lang.NullPointerException",
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "reason": "backendError"
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: }
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: ],
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: "message": "java.lang.NullPointerException"
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: }
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
07-12 14:09:57.935 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends$override.doInBackground(EndpointTask.java:101)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends$override.access$dispatch(EndpointTask.java)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends.doInBackground(EndpointTask.java:0)
07-12 14:09:57.936 9777-9830/com.hb.birthpay W/System.err: at com.hb.birthpay.utils.EndpointTask$GetFriends.doInBackground(EndpointTask.java:68)
Here is the stacktrace for the backend: 这是后端的堆栈跟踪 :
java.lang.NullPointerException
at com.hb.backend.spi.BirthpayEndpoint.getFriends(BirthpayEndpoint.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Why is this happening and how do I fix it? 为什么会发生这种情况,我该如何解决?
The NPE appears to be happening from the line: NPE似乎正在从线上发生:
List<String> friendIds = profile.getFriendIds();
This means that profile
is null
, which from this code will happen if the user ID does not result in a Profile
object being fetched from your datastore. 这意味着
profile
为null
,如果用户ID不会导致从数据存储区中获取Profile
对象,则会发生此代码。 What is mUserId
set to and is it a valid ID on the backend? 什么是
mUserId
设置,它是后端的有效ID吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.