Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

android - Retrieving data using Map from Firebase Database

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...