ユーザID、ユーザ名、およびフォトURL情報をFirebaseデータベースに保存しようとしています。私は、これは私のUserDaoクラスであるなど、民間分野、ゲッター/セッター、とユーザーPOJOクラスを持っている:Firebaseデータベースにユーザ情報を保存する
public class UserDao {
private static final String USERS_TAG = "users";
private FirebaseDatabase db = FirebaseDatabase.getInstance();
private Map<String, User> users;
private User currentUser;
....
public void write(User user){
db.getReference(USERS_TAG + "/" + user.getId()).setValue(user);
}
public boolean userExists(String userId){
return users != null && users.containsKey(userId);
}
....
}
これはRegisterEmailPasswordActivityクラスです:
public class RegisterEmailPasswordActivity extends BaseActivity {
....
public void registerUserWithEmailPassword(View view) {
if(view == buttonRegister){
String email = editTextEmail.getText().toString();
String password = editTextPassword.getText().toString();
if(TextUtils.isEmpty(email)){
Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
return;
}
progressDialog.setMessage("Registering user... ");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
storeToDb();
Toast.makeText(RegisterEmailPasswordActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
startActivity(new Intent(RegisterEmailPasswordActivity.this, MainActivity.class));
}else{
Toast.makeText(RegisterEmailPasswordActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
private void storeToDb(){
final FirebaseUser firebaseUser = FirebaseAuth
.getInstance().getCurrentUser();
if (userDao.userExists(firebaseUser.getUid())) {
userDao.setCurrentUser(userDao.getUserById(firebaseUser.getUid()));
} else {
final User user = new User(firebaseUser.getUid(),
firebaseUser.getDisplayName(),
firebaseUser.getPhotoUrl().toString());
userDao.write(user);
userDao.setCurrentUser(user);
}
}
}
私はこの例外取得しています:
をjava.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity.storeToDb(RegisterEmailPasswordActivity.java:107)
at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity.access$000(RegisterEmailPasswordActivity.java:25)
at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity$1.onComplete(RegisterEmailPasswordActivity.java:82)
at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
ここで私は何を間違えていますか?
後で私は、ユーザーを投稿や他のモデルに関連付けることにします。 – Nemus