I am trying to retrieve data from firebase database in Map format but I am getting a null pointer exception.I have successfully inserted data using map into my firebase database.
Here is my code:
public class Chat extends AppCompatActivity {
DatabaseReference databaseReference1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
Firebase.setAndroidContext(this);
databaseReference1 = FirebaseDatabase.getInstance().getReferenceFromUrl("URL");
databaseReference1.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
@Override
public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
Map map = (Map) dataSnapshot.getValue();
String message = map.get("text").toString(); //ERROR GOES HERE!!
String userName = map.get("Username").toString();
}
@Override
public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…