![](/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.