Possible solution, but structure failureIn my app there is a list I want to retrieve from Firebase Realtime Database, but at the second I add something on the database the list on the app does not actualize, it crashes. Whats wrong with the ValueListener?
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
comment = ds.getValue(Comment.class);
list.add(comment.getCommentText().toString()+" -"+comment.getUser().toString());
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference at world.cryneo.partytime.java.EventProfileActivity$1.onDataChange(EventProfileActivity.java:71)
After the app crashes and restarts the new comment from the database is shown. But firstly it crashes.
This is what is getting uploaded. After upload the app crashs, but after restart of app the content is normally shown. So why crash?
public void uploadComment(){
String nextIndex = Long.toString(eventCommentsCounter+1);
newComment = input_event_comment.getEditText().getText().toString();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Events").child(event_name).child("comments").child(nextIndex).child("user");
mDatabase.setValue(userName);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Events").child(event_name).child("comments").child(nextIndex).child("commentText");
mDatabase.setValue(newComment);
finish();
Intent eventIntent = new Intent(WriteCommentActivity.this, EventProfileActivity.class);
eventIntent.putExtra("Event_Name", event_name);
startActivity(eventIntent);
finish();
}