Richard Peterson Richard Peterson - 10 months ago 141
Android Question

firebase dataSnapshot.getValue() crashes android app

I've put together a very simple android app to get used to using firebase on android. Following the getting started part of firebase's docs I am able to write to the database with no issue. But when I go to retrieve that data with getValue from the data base the app crashes.

I try to read the database with with following code

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");

ValueEventListener chatMessageListener = new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
TextView messages = (TextView) findViewById(;
chatMessage ChatMessage = dataSnapshot.getValue(chatMessage.class);
String message = dataSnapshot.toString();

public void onCancelled(DatabaseError error) { //....

The JSON is very simple:
this is taken from firebase, this data was provided from the app

I've also created "chatMessage" that was used for the writing the data:

private static class chatMessage {
public String message;
public String messageFrom;
public String messageTo;

public chatMessage() { }

public chatMessage(String message, String messageFrom, String messageTo) {
this.message = message;
this.messageFrom = messageFrom;
this.messageTo = messageTo;

public String getMessage(){
return message;
public String getMessageFrom(){
return messageFrom;
public String getMessageTo(){
return messageTo;


I see some issues with your code.

First your chatMessage class was not written in the java patterns. Every class name must start with an upper case letter.

private static class ChatMessage {

Your two constructors must also start with a capital letter

public ChatMessage() {  }

And in your onDataChange

ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);

also, you should be getting your message string using your ChatMessage object

String message = chatMessage.getMessage();