Reputation: 63
I am a beginner in FireBase database. I wrote this code for retrieving user data after authentication. The authentication part is working well but the data retrieving part is crashing my app with this error.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.kcw.firebasebasics.UserInterface$1.onDataChange(UserInterface.java:58)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
Here is the code that I wrote and I wrote a comment on line 58 (the line causing error) to identify it.
FirebaseUser firebaseUser;
DatabaseReference reference;
TextView greeting;
String name,text;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_interface);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference()
.child(firebaseUser.getUid());
greeting = findViewById(R.id.greet_msg);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue().toString();//line 58
try {
JSONObject object = new JSONObject(text);
name = object.getString("username");
} catch (JSONException e) {
e.printStackTrace();
}
String message = "WELCOME ";
message = message + name.toUpperCase();
greeting.setText(message);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Help me resolve this.
I verified whether there is any null object in my database and I didn't find any.
I also tried making all my String variables global but that didn't work either
And finally a picture of my database after registering users.
Upvotes: 1
Views: 47
Reputation: 5841
You have wrong path in the database reference
, it should be under users
node :
reference = FirebaseDatabase.getInstance().getReference()
.child("users").child(firebaseUser.getUid());
greeting = findViewById(R.id.greet_msg);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exist()) {
String text = dataSnapshot.getValue().toString();//line 58
try {
JSONObject object = new JSONObject(text);
name = object.getString("username");
} catch (JSONException e) {
e.printStackTrace();
}
String message = "WELCOME ";
message = message + name.toUpperCase();
greeting.setText(message);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 0
Reputation: 317928
According to the API documentation, getValue() returns null when there is no data at the location you queried. You should check if data actually exists() there before calling getValue(), or at least manually check for null before calling toString().
Upvotes: 1