0

I'm trying to set my TextView to the current user username but i'm getting this error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

and this is how i'm retrieving the username:

mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

               String name= dataSnapshot.child("username").getValue().toString();
               username.setText(name);

           }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("ProfileActivity", "load:onCancelled", databaseError.toException());

        }
    });

this is my user node:

users firebase

this:

 mCurrUser = FirebaseAuth.getInstance().getCurrentUser();
 Uid = mCurrUser.getUid();
 mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(Uid);
Afaf Matg
  • 39
  • 7

1 Answers1

0

dataSnapshot.child("username").getValue() is returning null, which means that there is no child called "username" at the location of your query. From your comments, it sounds like you're building mDatabase like this:

mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(Uid);

The problem here is that your code is accessing "Users" with a capital U, but the data you're showing in the database exists at "users" with a lowercase u.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441