David Bowen David Bowen - 3 years ago 268
Java Question

Firebase:Failed to convert value of type java.lang.Boolean to String

Hi am trying to check in my database for a boolean of false, however I am getting an error of Failed to convert value of type java.lang.Boolean to String.

Structure of DB

Query q = databaseReference.child("Chatmessages").child(currentChatUser).child(userId);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String true3 = "true";
for(DataSnapshot ds : dataSnapshot.getChildren()) {


if(ds.child("read").getValue(String.class).equals(false)){
ds.child("read").getRef().setValue(true);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
q.addListenerForSingleValueEvent(eventListener);

Answer Source

Your comparison is wrong:

ds.child("read").getValue(String.class).equals(false)

There's no way that the String value that you read will ever be true to the Boolean value false.

Either do a string to string comparison:

ds.child("read").getValue(String.class).equals("false")

Or convert the string to a boolean first and then compare:

Boolean.valueOf(ds.child("read").getValue(String.class)).equals(false)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download