0

A NullPointerException occurs on the indicated line of my endpoint api method when called by the android client but not when called from the api explorer:

@ApiMethod(name = "publishReview", path = "publish-review", httpMethod = ApiMethod.HttpMethod.POST)
public Review publishReview(@Named("userId") final String id, ReviewForm reviewForm) {
    Key<Profile> profileKey = Key.create(Profile.class, id);

    final Key<Review> reviewKey = factory().allocateId(profileKey, Review.class);

    final Long reviewId = reviewKey.getId();

    Profile user = ofy().load().key(profileKey).now();

    Review review = new Review(reviewId, id, reviewForm);
    user.addToMyReviews(reviewId); // NULLPOINTER HERE
    ofy().save().entities(review, user).now();

    return review;
}

Here is addToMyReviews(Long reviewId):

public void addToMyReviews(final Long reviewId) {
    if (!myReviews.contains(reviewId))
        myReviews.add(reviewId);
}

Here is the android client side call of the endpoint method:

public static class PublishReview extends AsyncTask<Void, Void, String> {

    private static MyApi myApiService = null;
    private ReviewForm mReview;
    private final String mUserId;
    private Context mContext;

    public PublishReview(final String userId, ReviewForm review, Context context) {
        mReview = review;
        mUserId = userId;
        mContext = context;
    }

    @Override
    protected String doInBackground(Void... params) {
        if (myApiService == null) {  // Only do this once
            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    // options for running against local devappserver
                    // - 10.0.2.2 is localhost's IP address in Android emulator
                    // - turn off compression when running against local devappserver
                    .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
            myApiService = builder.build();
        }

        try {
            return myApiService.publishReview(mUserId, mReview).execute().getTitle();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String title) {
        Toast.makeText(mContext, title + " published", Toast.LENGTH_LONG).show();
    }
}

The mUserId and mReview variables on the client side are not null when passed into the endpoint method as params.

How do I fix this error?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54
  • Are you sure you're calling the same app instance? Have you logged the variables to see that they're not only not null but identical between calls? – saiyr Oct 07 '16 at 17:40
  • I have debugged the backend and all the variables are identical and correct, this is really confusing. – Tom Finet Oct 07 '16 at 17:56
  • What line is the `NullPointerException` being thrown by? (Can you share a stack trace with us?) – Will Hayworth Oct 07 '16 at 21:49
  • java.lang.NullPointerException at com.neutronstar.backend.Endpoint.publishReview(Endpoint.java:62) 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) @Will Hayworth – Tom Finet Oct 08 '16 at 12:43
  • Rad! Which line is 62 in the code above? – Will Hayworth Oct 09 '16 at 03:54
  • user.addToMyReviews(reviewId); @Will Hayworth – Tom Finet Oct 09 '16 at 07:01
  • Okay, so two thoughts: 1. What does the ofy()... bit do? I can't see an ofy() method anywhere in the code you've posted. It looks like `user` is null, so the `.now()` bit at the end of that line is returning a null value. 2. It's unlikely to be related but your null-initializer pattern for `myApiService` isn't threadsafe. If you want/need to fix that, here are some options: http://stackoverflow.com/questions/8297705/how-to-implement-thread-safe-lazy-initialization – Will Hayworth Oct 10 '16 at 20:53

0 Answers0