![](/img/trans.png)
[英]How to fix NullPointerException in post request - Google Cloud Endpoints?
[英]Google cloud endpoints method always causes NullPointerException, why?
我正在开发一个使用谷歌端点作为后端的Android应用程序。 出于某种原因,其中一个端点方法只在从android客户端调用时才会导致java.lang.NullPointerException
,但在从api资源管理器调用时则不会。
这是端点方法:
@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;
}
这是配置文件实体getFriendIds()方法:
public List<String> getFriendIds() {
return friendIds != null ? ImmutableList.copyOf(friendIds) : null;
}
在调试模式和API资源管理器中,一切都按预期工作。 在这个例子中,我创建了2个用户,一个id = 4567
,另一个id = 9876
。 以下是我运行方法获取朋友时发生的事情:
它按预期工作,并且在api explorer调用时不会产生任何错误。
然后,当用户没有朋友时,我尝试了它:
它按预期工作,并且在api explorer调用时不会产生任何错误。
在我的Android客户端中,我调用这个确切的方法,但该方法总是会导致java.lang.NullPointerException
。
我从异步任务中调用它,如下所示:
return mApi.getFriends(mUserId).execute().getItems();
这是客户端的堆栈跟踪 :
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)
这是后端的堆栈跟踪 :
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)
为什么会发生这种情况,我该如何解决?
NPE似乎正在从线上发生:
List<String> friendIds = profile.getFriendIds();
这意味着profile
为null
,如果用户ID不会导致从数据存储区中获取Profile
对象,则会发生此代码。 什么是mUserId
设置,它是后端的有效ID吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.