Google cloud endpoints method always causes NullPointerException, why?

5 views
Skip to first unread message

Tom Finet via StackOverflow

unread,
Jul 12, 2016, 9:49:10 AM7/12/16
to google-appengin...@googlegroups.com

I am developing an android app which used google endpoints as a backend. 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.

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:

public List<String> getFriendIds() {
    return friendIds != null ? ImmutableList.copyOf(friendIds) : null;
}

In debug mode and in the API explorer, everything works as expected. In this example I created 2 users, one with id = 4567 and the other id = 9876. Here is what happened when I ran the method to get the friends:

enter image description here

It works as expected and caused no errors when called by api explorer.

I then tried it when the user has no friends:

enter image description here

It works as expected and caused no errors when called by api explorer.

In my android client I call this exact method but the method always causes a 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?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/38330694/google-cloud-endpoints-method-always-causes-nullpointerexception-why

Larry Schiefer via StackOverflow

unread,
Jul 12, 2016, 10:09:11 AM7/12/16
to google-appengin...@googlegroups.com

The NPE appears to be happening from the line:

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. What is mUserId set to and is it a valid ID on the backend?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/38330694/google-cloud-endpoints-method-always-causes-nullpointerexception-why/38331052#38331052
Reply all
Reply to author
Forward
0 new messages