Sanket
Sanket

Reputation: 51

Null pointer exception when doing a "get" in Google Cloud Endpoints (App Engine Backend) and objectify on Android App

I followed the tutorial given here to develop my code. Since the whole code is given at the above link as well as here I wont type it all here to avoid cluttering.

On the app engine app, I have a class like this :

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class Quote {
 @Id
 Long id;
 String who;
 String what;

public Quote() {}

public Long getId() {
 return id;
 }

public void setId(Long id) {
 this.id = id;
 }

public String getWho() {
 return who;
 }

public void setWho(String who) {
 this.who = who;
 }

public String getWhat() {
 return what;
 }

public void setWhat(String what) {
 this.what = what;
 }
}

And since I automatically generated my Endpoints class from the above class, it looks (partly) like this (As opposed to the one shown in the example)

@Api(
        name = "quoteApi",
        version = "v1",
        resource = "quote",
        namespace = @ApiNamespace(
                ownerDomain = "QuoteApi.com",
                ownerName = "QuoteApi.com",
                packagePath = ""
        )
)
public class QuoteEndpoint {

    private static final Logger logger = Logger.getLogger(QuoteEndpoint.class.getName());

    private static final int DEFAULT_LIST_LIMIT = 20;

    static {
        // Typically you would register this inside an OfyServive wrapper. See: https://code.google.com/p/objectify-appengine/wiki/BestPractices
        ObjectifyService.register(Quote.class);
    }

    /**
     * Returns the {@link Quote} with the corresponding ID.
     *
     * @param id the ID of the entity to be retrieved
     * @return the entity with the corresponding ID
     * @throws NotFoundException if there is no {@code Quote} with the provided ID.
     */
    @ApiMethod(
            name = "get",
            path = "quote/{id}",
            httpMethod = ApiMethod.HttpMethod.GET)
    public Quote get(@Named("id") Long id) throws NotFoundException {
        logger.info("Getting Quote with ID: " + id);
        Quote quote = ofy().load().type(Quote.class).id(id).now();
        if (quote == null) {
            throw new NotFoundException("Could not find Quote with ID: " + id);
        }
        return quote;
    }
}

Now in my Android App, I do this. This goes in an AsyncTask, doinbackground

    try {
        Long id = Long.getLong("9649299534313129");
        Quote q1= endpointBldr.get(id).execute();

    }catch (IOException e){
        return e.getMessage();
    }

I should mention, the "id" that I am asking to get, is accurate, and exists in the datastore. Also, the 'list' and 'insert' and 'delete' ApiMethods work just fine. Just this 'get' is giving me trouble. All of which are auto-generated methods.

And finally the Error I get , that stops my app is:

/com.blah.QuoteApi E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.blah.QuoteApi, PID: 16820
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

Caused by: java.lang.NullPointerException: Required parameter id must be specified.
            at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:208)
            at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:140)
            at com.blah.QuoteApi$Get.<init>(Quote.java:151)
            at com.blah.QuoteApi.QuoteApi.get(QuoteApi.java:129)
            at com.blah.QuoteApi.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:72)
            at com.blah.QuoteApi.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:22)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

Can someone please help me resolve this. Not sure where it is getting a null pointer , I give the assigned Long id, as argument to the get.

Also, should point out, the ApiMethod works as it should in the online Google Api Explorer (AppSpot)

Upvotes: 2

Views: 1232

Answers (1)

Sanket
Sanket

Reputation: 51

I must admit, it was a silliest mistake. Long.getLong is not the right one to use. Long.parseLong should have been the one. getLong was obviously gonna give null in this case.

Upvotes: 3

Related Questions