-2

My firebase database structure looks like this:

1

I'm using this code but it doesn't retrieving any data from database and sometime the getUid() produce NullPointerException. Can anyone help me to solve this problem?

mAuth = FirebaseAuth.getInstance();

mUser = mAuth.getCurrentUser();

userID = mUser.getUid();

mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

mDatabaseReference.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        showData(dataSnapshot);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

private void showData(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds: dataSnapshot.getChildren()) {
        UserInformation uInfo = new UserInformation();
        uInfo.setFullName(ds.child(userID).getValue(UserInformation.class).getFullName());
        uInfo.setUsername(ds.child(userID).getValue(UserInformation.class).getUsername());
        uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail());
        
        fullNameProfile.setText(uInfo.getFullName());
        usernameProfile.setText(uInfo.getUsername());
        emailProfile.setText(uInfo.getEmail());
    }
}
flaxel
  • 4,173
  • 4
  • 17
  • 30

1 Answers1

1

To simply get user data from the database please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String email = dataSnapshot.child("Email").getValue(String.class);
        String fullName = dataSnapshot.child("FullName").getValue(String.class);
        String password = dataSnapshot.child("Password").getValue(String.class);
        String userName = dataSnapshot.child("Username").getValue(String.class);
        Log.d(TAG, userName);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

Or even simpler using a UserInformation class:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        UserInformation uInfo = dataSnapshot.getValue(UserInformation.class);
        Log.d(TAG, uInfo.getUsername());
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I tried your solution but it didn't working for me. Can you give me perfect solution to retrieve data all the data from firebase like fullname, username and email as specified in my database structure. – Dweep Panchal Feb 08 '19 at 12:07
  • Let's use the first solution, in that case, why do you say it didn't work? Do you have an error? What is the behaviour? See also my edited answer for the other properties. This is the solution that should 100% work, right? – Alex Mamo Feb 08 '19 at 12:36
  • It didn't producing any error. I use both the solutions but it didn't getting data from database. It showing nothing in Log. If you say i will give you github link of my code. – Dweep Panchal Feb 08 '19 at 15:21
  • Is `onDataChange` method even triggered? If you add a log statement (`Log.d(TAG, "Test!!!!!");`) inside it does it print something? – Alex Mamo Feb 08 '19 at 15:22
  • No its not triggering. – Dweep Panchal Feb 08 '19 at 15:32
  • So it's not aboute the code I provided you. Most likely in such cases is that you don't have permissions. What does this line `Log.d(TAG, databaseError.getMessage());` print? Are also sure you have internet acess on your device? – Alex Mamo Feb 08 '19 at 15:34
  • My device has full access of internet and i also provided internet permission to app in Manifest file. `Log.d(TAG, databaseError.getMessage());` this line printing nothing. Am thinking that ValueEventListener is not working. – Dweep Panchal Feb 08 '19 at 15:39
  • Please provide me the code that you are using, Github or edit your question with the relevant code. – Alex Mamo Feb 08 '19 at 15:41
  • Am providing github link wait. – Dweep Panchal Feb 08 '19 at 15:43
  • Ok, add it as a comment. – Alex Mamo Feb 08 '19 at 15:44
  • Hey good news its working but it only retrieving data when i create new account, it doesn't retrieving existing user data. – Dweep Panchal Feb 08 '19 at 16:08
  • Good to hear that it worked :) It's normal to be able to get data only when data exist in the database. So using the above code, you be able to get the data of the logged in user. Isn't this what you want? – Alex Mamo Feb 08 '19 at 16:10
  • When i create new user it gets data from database but if i logged with existing user which has there data in database its not retrieving data sometime and sometime its gets data. – Dweep Panchal Feb 08 '19 at 16:24
  • This sounds as a new issue which basically should be considered another question that cannot be answered in a comment or using only the data above. As a personal guess, I think that you are using a wrong database reference but in order to follow the rules of this comunity, please post another fresh question using a [MCVE](https://stackoverflow.com/help/mcve), so me and other Firebase developers can help you. – Alex Mamo Feb 08 '19 at 16:28
  • Yaa thanks for your time and help. One small question "Why getUid() produces nullPointerException sometime?". – Dweep Panchal Feb 08 '19 at 16:31
  • Maybe because of **[that](https://stackoverflow.com/questions/54069574/firebase-android-listview-not-being-displayed)**. – Alex Mamo Feb 08 '19 at 16:32
  • Thankyou so much!!! – Dweep Panchal Feb 08 '19 at 16:35