0

[This is my MainActivity.java file][1]

package com.example.socialmediaintegration;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

private LoginButton loginButton;
private CircleImageView circleImageView;
private TextView txtName, txtEmail;

private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // private static final String EMAIL = "email";

    loginButton =  findViewById(R.id.login_button);
    circleImageView = findViewById(R.id.profile_pic);
    txtName = findViewById(R.id.profile_name);
    txtEmail = findViewById(R.id.profile_email);

    callbackManager = CallbackManager.Factory.create();
    loginButton.setPermissions(Arrays.asList("email", "public_profile"));
    checkLoginStatus();


    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
}

AccessTokenTracker tokenTracker = new AccessTokenTracker() {
    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

        if (currentAccessToken == null)
        {
            txtName.setText("");
            txtEmail.setText("");
            circleImageView.setImageResource(0);
            Toast.makeText(MainActivity.this, "User Logged out", Toast.LENGTH_LONG).show();
        }
        else {
            loadUserProfile(currentAccessToken);
        }
    }
};

private void loadUserProfile(AccessToken newAccessToken){
    GraphRequest request = GraphRequest.newMeRequest(newAccessToken, new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {
            try {
                String first_name = object.getString("first_name");
                String last_name = object.getString("last_name");
                String email = object.getString("email");
                String id = object.getString("id");

                String image_url =  "http://graph.facebook.com/"+id+"/picture?type=normal";

                txtEmail.setText(email);
                txtName.setText(first_name + " " + last_name);
                RequestOptions requestOptions = new RequestOptions();
                requestOptions.dontAnimate();

                Glide.with( MainActivity.this).load(image_url).into(circleImageView);

            } catch (JSONException e){
                e.printStackTrace();
            }


        }
    });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "first_name, last_name, email_id");
    request.setParameters(parameters);
    request.executeAsync();
}

private void checkLoginStatus(){

    if (AccessToken.getCurrentAccessToken()!=null)
    {
        loadUserProfile(AccessToken.getCurrentAccessToken());
    }
}

}

This is my error shown in Logcat-->>

2020-10-12 12:01:23.621 19597-19602/com.example.socialmediaintegration I/zygote: Increasing code cache capacity to 256KB 2020-10-12 12:01:27.373 19597-19623/com.example.socialmediaintegration E/GraphResponse: {HttpStatus: 400, errorCode: 100, subErrorCode: 33, errorType: GraphMethodException, errorMessage: Unsupported get request. Object with ID '980066249173751' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api} 2020-10-12 12:01:32.329 19597-19623/com.example.socialmediaintegration E/GraphResponse: {HttpStatus: 400, errorCode: 100, subErrorCode: -1, errorType: OAuthException, errorMessage: (#100) Tried accessing nonexisting field (email_id) on node type (User)} 2020-10-12 12:01:32.329 19597-19597/com.example.socialmediaintegration D/AndroidRuntime: Shutting down VM 2020-10-12 12:01:32.332 19597-19597/com.example.socialmediaintegration E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.socialmediaintegration, PID: 19597 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference at com.example.socialmediaintegration.MainActivity$3.onCompleted(MainActivity.java:102) at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:317) at com.facebook.GraphRequest$5.run(GraphRequest.java:1398) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6626) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

rootcause
  • 1
  • 1

1 Answers1

0

In this error line

Attempt to invoke virtual method 'java.lang.String 
org.json.JSONObject.getString(java.lang.String)' on a null object reference

It's said that your JSONObject instance is null and throw NullPointerException

Before reading data from JSONObject you need first to check if the request is successful or not

Check Handling Errors section

https://developers.facebook.com/docs/android/graph

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30