0

私はFacebookのGraphRequestを使ってユーザーの詳細を取得し、それらの詳細をFirebaseに保存したり、sharedPreferencesにローカルで保存したりしています。これは、コードの一部です: -GraphRequest、executeAsync、Android:なぜNullPointerExceptionがありますか?

public class Login extends AppCompatActivity { 

Profile userProfile; /* Profile is a user-created class */ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    facebookLoginButton.registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      GraphRequest request = GraphRequest.newMeRequest(
        loginResult.getAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback() { 
         @Override 
         public void onCompleted(JSONObject object, GraphResponse response) { 
          String fullName,email; 
          try { 
           fullName=object.getString("name"); 
           email = object.getString("email"); 
           /* Initializing userProfile */ 
           userProfile=new Profile(fullName,email); 
           firebaseReference.setValue(userProfile); //This works, which means userProfile is NOT null here 
          } catch (JSONException e) { 
           Log.d("JSONerror",e.toString()); 
          } 
         } 
        }); 

      Bundle parameters = new Bundle(); 
      parameters.putString("fields", "id,name,email"); 
      request.setParameters(parameters); 
      request.executeAsync(); 

      saveProfileInfoLocally(); 
     } 
     @Override 
     public void onCancel() {} 
     @Override 
     public void onError(FacebookException error) {} 
    }); 

void saveProfileInfoLocally(){ 
    SharedPreferences sharedPrefs = getSharedPreferences("UserInfo", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPrefs.edit(); 
    editor.putString("fullName",userProfile.getFullName()); //BUT this does NOT work, says userProfile is null 
    editor.apply(); 
}} 

しかし、私は(saveProfileInfoLocallyにUSERPROFILEの詳細にアクセスしようとすると)、それが「nullオブジェクト参照で仮想メソッドを呼び出すようにしよう」と言って、NullPointerExceptionがスローされます。何が間違っているのかを教えてください。ありがとう。

答えて

0

新しいGraphRequest.GraphJSONObjectCallback()の外でsaveProfileInfoLocally()を呼び出すのは、実際にプロファイルを取得するコールバックであるからです。でも、なぜそれが「USERPROFILE」がnullであると言うん - 応答のため

@Override 
    public void onSuccess(LoginResult loginResult) { 
     GraphRequest request = GraphRequest.newMeRequest(
       loginResult.getAccessToken(), 
       new GraphRequest.GraphJSONObjectCallback() { 
        @Override 
        public void onCompleted(JSONObject object, GraphResponse response) { 
         String fullName,email; 
         try { 
          fullName=object.getString("name"); 
          email = object.getString("email"); 
          /* Initializing userProfile */ 
          userProfile=new Profile(fullName,email); 

          saveProfileInfoLocally(); 

          firebaseReference.setValue(userProfile); //This works, which means userProfile is NOT null here 
         } catch (JSONException e) { 
          Log.d("JSONerror",e.toString()); 
         } 
        } 
       }); 

    } 
+0

おかげで、しかし、私は理解していないことである。

次の例のように、コールバック内のメソッド呼び出しを移動する必要があります私はそれを初期化しましたが? – rdx

+0

初期化していないため、 プロファイルを初期化したコードは、コールバックの中にあります。また、コールバックは非同期に発生するものを定義します。それは今、または今から10分後に起こる可能性があることを意味します。しかし、コールバックの外側でsaveProfileInfoLocally()メソッドを呼び出していました。つまり、GraphJSONObjectCallbackが完了したかどうかを知らずに、コードがProfileオブジェクトを保存しようとしていました。 明らかに、コールバックは完了せず、プ​​ロファイルはnullです:) – Cbr

+0

私はこれを理由として考えましたが、私はこの行と混同しています - firebaseReference.setValue(userProfile) - 値を設定し、データベース内のデータ。これは、userProfileが作成されており、nullでないことを意味します。私は正しいですか? – rdx

関連する問題