0

I am using dataSnapshot to get a value from my Firebase Database, but I keep getting a null reference error. The path to my firebase is correct and I have Datasnapshot defined, so I am not too sure why this error is coming up.

Here is the snippit that is giving me issues

    User user = new User();
    String username;
    username = (String) datasnapshot.child("Users").child(uid).child("userName").getValue();
    user.setusername(username);
    navuserName.setText(username);

and this is the error that it is giving me.

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DataSnapshot com.google.firebase.database.DataSnapshot.child(java.lang.String)' on a null object reference

I have Datasnapshot dataSnapshot; defined in the class but the error is being thrown. Any suggestions?

brent
  • 131
  • 12

1 Answers1

0

Your variable dataSnapshot is not initialized. It's value is null. You need to initialize the variable before you can use it's methods.

Pim
  • 554
  • 3
  • 12