【问题标题】:Google cloud endpoints method always causes NullPointerException, why?谷歌云端点方法总是导致 NullPointerException,为什么?
【发布时间】:2023-04-01 10:15:01
【问题描述】:

我正在开发一个使用谷歌端点作为后端的安卓应用。出于某种原因,其中一个端点方法只会在从 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;
}

在调试模式和 API 资源管理器中,一切正常。在这个例子中,我创建了 2 个用户,一个是 id = 4567,另一个是 id = 9876。以下是我运行获取好友的方法时发生的情况:

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

然后我在用户没有朋友的时候试了一下:

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

在我的 android 客户端中,我调用了这个确切的方法,但该方法总是导致 java.lang.NullPointerException

我从这样的异步任务中调用它:

return mApi.getFriends(mUserId).execute().getItems();

这是客户端的 stacktrace

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)

这是后端的 stacktrace

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)

为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: java android google-app-engine null google-cloud-endpoints


    【解决方案1】:

    NPE 似乎正在发生:

    List<String> friendIds = profile.getFriendIds();
    

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

    【讨论】:

    • 为了验证我的用户,我使用了 firebase,它返回与该用户关联的令牌。我将其保存在谷歌数据存储中作为配置文件对象 ID。 @Larry Schiefer
    • 现在检查 mUserId 是否实际上是数据存储中的 id。 @Larry Schiefer
    • 是的,你是对的,mUserId = com.google.android.gms.tasks.zzh@d80f63b 保存时但调用获取朋友方法时 mUserId = com.google.android.gms.tasks.zzh @5f8d010。
    • 很高兴你能找到它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多