简体   繁体   中英

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

I am trying to read data from Firebase but it isn't working for me. There's already data in the database but it's saying the value is null when I use getUserFName. So the second code is from the register activity, which creates an authentication and an id for the database to be matched together. the id holds the data for the class UserInformation. So that's why I am wondering why I am getting a null object when there's data when I register.

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

                                            }
                                        });

You are reading from a wrong location.

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

Also make sure you have read permission in Firebase database as in https://firebase.google.com/docs/database/security/quickstart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM