Tommz
Tommz

Reputation: 3453

Facebook SDK for Android null pointer exception

I'm following simple tutorial at: https://developers.facebook.com/docs/android/getting-started/ and after I made everything work, I have problems onCompleted callback method.

My code looks like this:

Request.newMeRequest(session, new Request.GraphUserCallback() {

              // callback after Graph API response with user object
              @Override
              public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
                  TextView welcome = (TextView) findViewById(R.id.welcome);
                  welcome.setText("Hello " + user.getName() + "!");
                }
              }
            }).executeAsync();

since executeMeRequestAsync method from tutorial was deprecated. Everything passes fine, and user != null evaluates as true so I come inside of the block, but on user.getName() I always get NullPointerException and I've checked while debugging GraphUser instance, and it was filled with null values. What I might be doing wrong? Can it be some problems with application configuration? I've generated new KeyHash and it's correct, so I don't know what else would be incorrect.

Upvotes: 0

Views: 479

Answers (1)

Chris Forrence
Chris Forrence

Reputation: 10084

A NullPointerException can be thrown if findViewById(R.id.welcome) returns null. You can modify your logic to check for welcome as well:

public void onCompleted(GraphUser user, Response response) {
    TextView welcome = (TextView) findViewById(R.id.welcome);

    if(user != null && welcome != null) {
        welcome.setText("Hello " + user.getName() + "!");
    }
    else {
        // Log it in some way
    }
}

Upvotes: 1

Related Questions