0

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

                                            }
                                        });
mike-gallego
  • 706
  • 2
  • 12
  • 27
  • post the database structure and code you used to create your reference to it , not the down-voter but reason it incomplete details – Pavneet_Singh Jun 30 '17 at 17:23
  • Possible duplicate https://stackoverflow.com/questions/43261522/unable-to-read-data-from-firebase-in-android – Animesh Patra Jun 30 '17 at 17:33
  • Most likely this means that you're reading from the wrong location, one that doesn't have contain user information. In that case post the code that initializes `databaseReference` and show the JSON from your database as that location (as text, no screenshots), which get by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 30 '17 at 17:37
  • It could also be that you don't have permission to read the data, which you can detect by following these instructions: https://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database/22652/detect-errors-when-reading-data-on-android#t=201706301736486318537 – Frank van Puffelen Jun 30 '17 at 17:37

1 Answers1

2

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

ram
  • 483
  • 1
  • 4
  • 11