gpt4 book ai didi

java - Google 云端点方法总是引发 NullPointerException,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:36 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它使用 google 端点作为后端。由于某种原因,其中一个端点方法仅在从 Android 客户端调用时才会导致 java.lang.NullPointerException,但在从 api explorer 调用时则不会。

这是端点方法:

@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;
}

在 Debug模式和 API 资源管理器中,一切都按预期运行。在此示例中,我创建了 2 个用户,其中一个的 id = 4567,另一个 id = 9876。这是我运行该方法来获取 friend 时发生的情况:

enter image description here

它按预期工作,并且在 api explorer 调用时没有导致错误。

然后当用户没有 friend 时我尝试了:

enter image description here

它按预期工作,并且在 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();

这意味着 profilenull,如果用户 ID 未导致从数据存储区获取 Profile 对象,则此代码中将会发生这种情况。 mUserId 设置为什么?它是否是后端的有效 ID?

关于java - Google 云端点方法总是引发 NullPointerException,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com