引起:java.lang.NullPointerException:尝试调用虚方法'java.lang.String com.facebook.Profile.getFirstName()'

时间:2017-07-19 12:17:05

标签: java android facebook

我正在尝试使用Facebook API获取我的Android验证的一些数据。我可以登录我的Facebook帐户。这是我设置的从Facebook帐户获取数据的权限

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

使用上述权限我试图获取配置文件名称和其他数据我得到一个空指针异常与下面的代码片段

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());

请问,我正在使用的方法有什么问题。

1 个答案:

答案 0 :(得分:0)

  

当应用程序尝试使用时抛出NullPointerException   具有空值的对象引用。

<强> getCurrentProfile()

  

获取当前登录到应用程序的配置文件。

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("---------------------");

   }