Edmond Tamas Edmond Tamas - 6 months ago 80
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!

Thanks!

exception:


Android Version: android version: 4.3

java.lang.NullPointerException at
com.cerculdivelor.SplashActivity$2$1.onCompleted(SplashActivity.java:90)
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
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at
dalvik.system.NativeStart.main(Native Method)


Method:

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

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
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);
editor.commit();

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

finish();

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


}

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

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

Answer

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:

@Override
public void onCompleted(JSONObject object, GraphResponse response) {

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

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