尝试从 firebase 接收数据时获取空对象引用

getting a null object reference when trying to receive data from firebase

我正在尝试从 Firebase 读取数据,但它对我不起作用。数据库中已经有数据,但它说当我使用 getUserFName 时该值为空。所以第二个代码来自寄存器activity,它创建了一个身份验证和一个数据库的id来匹配在一起。 id 保存 class UserInformation 的数据。所以这就是为什么我想知道为什么我在注册时有数据时得到一个空对象。

databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    UserInformation userInformation = new UserInformation();
                    userInformation.setUserFName(ds.child(userID).getValue(UserInformation.class).getUserFName());
                    userInformation.setUserLName(ds.child(userID).getValue(UserInformation.class).getUserLName());
                    userInformation.setUserEmail(ds.child(userID).getValue(UserInformation.class).getUserEmail());
                    userInformation.setUserDescription(ds.child(userID).getValue(UserInformation.class).getUserDescription());

                    fName.setText(userInformation.getUserFName());
                    lName.setText(userInformation.getUserLName());
                    email.setText(userInformation.getUserEmail());
                    aboutYou.setText(userInformation.getUserDescription());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
firebaseAuth.createUserWithEmailAndPassword(email.getText().toString().trim(), password.getText().toString().trim())
                                        .addOnCompleteListener(NewUserActivity.this, new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                if (task.isSuccessful()) {
                                                    Toast.makeText(NewUserActivity.this, "Account Created. Sign in!", Toast.LENGTH_SHORT).show();

                                                    String id = firebaseAuth.getCurrentUser().getUid();
                                                    UserInformation userInformation = new UserInformation(firstName.getText().toString(), lastName.getText().toString(), email.getText().toString(),
                                                            password.getText().toString());
                                                    databaseReference.child(id).setValue(userInformation);

                                                    Intent created = new Intent(NewUserActivity.this, LoginActivity.class);
                                                    startActivity(created);
                                                    finish();
                                                } else if (password.getText().toString().length() < 6){
                                                    Toast.makeText(NewUserActivity.this, "Password at least 6 characters", Toast.LENGTH_SHORT).show();
                                                    progressDialog.dismiss();
                                                } else{
                                                    Toast.makeText(NewUserActivity.this, "Email already exists", Toast.LENGTH_SHORT).show();
                                                    progressDialog.dismiss();
                                                }

                                            }
                                        });

您从错误的位置阅读。

databaseReference.child(userID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
        fName.setText(userInformation.getUserFName());
        lName.setText(userInformation.getUserLName());
        email.setText(userInformation.getUserEmail());
        aboutYou.setText(userInformation.getUserDescription());
    }
}

还要确保您在 Firebase 数据库中具有读取权限,如 https://firebase.google.com/docs/database/security/quickstart