Edmond Tamas Edmond Tamas - 8 months ago 111
Android Question

GraphRequest throwing NullPointerException when trying to get the ID of a user onCompleted?

Almost 95% of my users pass the login process without an issue, but there are times when I get this exception when I try: editor.putString("fbId", id). Why? If onSuccess gets called I assume the object would have been populated, then why the error or possible null respone??

Please help me out, I am getting crazy on this!



Android Version: android version: 4.3

java.lang.NullPointerException at
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304) at
com.facebook.GraphRequest$5.run(GraphRequest.java:1379) at
android.os.Handler.handleCallback(Handler.java:730) at
android.os.Handler.dispatchMessage(Handler.java:92) at
android.os.Looper.loop(Looper.java:176) at
android.app.ActivityThread.main(ActivityThread.java:5419) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:525) at
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at
dalvik.system.NativeStart.main(Native Method)


private void onFblogin() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(
"public_profile", "email"));

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());

// Application code
try {

/*** line 90 -->***/ String id = object.getString("id");
String name = object.getString("name");
String email = object.getString("email");
//String birthday = object.getString("birthday"); <-- error if people don't have this completed

Log.i("TAG", email + " " + id + " " + " " + name);

SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
editor.putString("fbId", id);
editor.putString("name", name);
editor.putString("email", email);

HttpConActivity http = new HttpConActivity(getApplication());
http.saveToServer(id, name, email);


} catch (JSONException e) {
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");


public void onCancel() {
Log.i("TAG", "Canceled!");

public void onError(FacebookException e) {
Log.i("TAG", "Error!");


Simply check for Errors in the GraphJSONObjectCallback - GraphResponse. The Documentation says, that JSONObject object can be null, so you should not assume that is not null.

Keep in mind that you communicate with a server. You should always expect errors and handle them.

So something like this:

public void onCompleted(JSONObject object, GraphResponse response) {

    FacebookRequestError error = response.getError();
    if (error != null) {
        // handle your error

    try {
        String id = object.getString("id");