1

I am trying to fetch some data on my android validation using Facebook API. I am able to login into my facebook account. This is the permission I have set to fetch data from Facebook account

loginButton.setReadPermissions(Arrays.asList( "public_profile", "email", "user_birthday", "user_friends"));

using the above permissions I am trying to fetch profile name and other data I am getting a null pointer exception with the below code snippet

public void getFacebookData(AccessToken accessToken, Profile profile) {
        System.out.println("---------------------");
        System.out.println("--Facebook Login Successful!");
        System.out.println("--Logged in user Details : ");
        System.out.println("---------------------");
        System.out.println("--User ID : " + accessToken.getUserId());
        System.out.println("--Authentication Token : " + accessToken.getToken());
        System.out.println("---------------------");
        System.out.println("--First Name : " + profile.getFirstName());
        System.out.println("--Last Name : " + profile.getLastName());
        System.out.println("--URL Perfil: " + profile.getLinkUri());
        System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500));
        System.out.println("---------------------");
        System.out.println("--ID : " + profile.getId());
        System.out.println("--Name : " + profile.getName());
        System.out.println("---------------------");

        TextView profile_name = (TextView)findViewById(R.id.profile);
        profile_name.setText(profile.getName());

Please, what be wrong with the approach I am using.

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
blend
  • 59
  • 1
  • 7

1 Answers1

0

NullPointerException is thrown when an application attempts to use an object reference that has the null value .

getCurrentProfile()

Getter for the profile that is currently logged in to the application.

public void getFacebookData(AccessToken accessToken, Profile profile) {
    System.out.println("---------------------");
    profile = Profile.getCurrentProfile(); // Add this 
   if (profile != null) 
   {
    System.out.println("--First Name : " + profile.getFirstName());
    System.out.println("--Last Name : " + profile.getLastName());
    System.out.println("--URL Perfil: " + profile.getLinkUri());
    System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500));
    System.out.println("---------------------");
    System.out.println("--ID : " + profile.getId());
    System.out.println("--Name : " + profile.getName());
    System.out.println("---------------------");

   }
livemaker
  • 464
  • 1
  • 9
  • 19
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198