tetutato tetutato - 6 months ago 161
Android Question

Firebase Android - query addChildEventListener crashes from array index out of bounds?

I'm trying to use Firebase with my Android app, and I'm trying to call the addChildEventListener to a database reference. However this reference may be empty initially (created but no child within it), which Firebase doesn't seem to check and I get an invalid index 0, size is 0 error and my app crashes.

query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


Is this a bug or am I doing something wrong?

EDIT: It was a bug on my part! The string variable i was using to reference the node was just null in some cases :P

Answer

FireBase returns dataSnapshot as null if it does not find any data with respect to particular node or if the node does not exist. So that is a normal behaviour only. PS: the above case is true in case of

 addValueEventListener and addListenerForSingleValueEvent

but in case of addChildEvent listener I don't thing that even onChild added callback should come. Please paste more code

Comments