DatabaseException: Can't convert object of type java.lang.String to type
You are getting this error:
Can't convert object of type java.lang.String to type com.parrot.sdksample.activity.CommandObject
Because you are trying to read the data of type String
which is of type CommandObject
and that why you are getting this error.
A more simple way to get those values would be to use the String
class like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference commandsRef = rootRef.child("drones").child("commands");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
boolean executed = ds.child("executed").getValue(Boolean.class);
String text = ds.child("text").getValue(String.class);
double timestamp = ds.child("timestamp").getValue(Double.class);
Log.d("TAG", executed + " / " + text + " / " + timestamp);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
commandsRef.addListenerForSingleValueEvent(eventListener);
And this the approach using an object of CommandObject
class:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference commandsRef = rootRef.child("drones").child("commands");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
CommandObject commandObject = ds.getValue(CommandObject.class);
Log.d("TAG", commandObject.getExecuted() + " / " +
commandObject.getText() + " / " +
commandObject.getTimestamp());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
commandsRef.addListenerForSingleValueEvent(eventListener);
In both cases your output will be:
false / TAKE_OFF / 1.512686825309134E9
false / LAND / 1.512687248764272E9
The solution provided by Alex worked but didn't quite answer why I couldn't get store my data in an object, and for my app while the child added listener, the app is able to read data that was already on Firebase but when new child is added, the value is empty.
onChildAdded:DataSnapshot { key = -L0JjGo-3QMYDsuTMQcN, value = }
I've done a little more digging to find out what might be causing this and found that it is probably caused because the values were not all written at the same time. And looking at firebase it seems that the key is added first, then the values are added. This is why the error says can't convert, it is because it doesn't exist. I'm using the Python Firebase Admin SDK to add the data to Firebase, so I'm not sure if this is the reason for that.
So to fix my problem I moved my code to the onChildChanged function and added a check so that the code only runs when all of the data I need exists. This way I can get the values stored in an object straight away.
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName){
if (dataSnapshot.child("text").exists() &&
dataSnapshot.child("executed").exists() &&
dataSnapshot.child("timestamp").exists()){
Log.d(TAG, "onChildChanged:" + dataSnapshot.toString());
CommandObject command = dataSnapshot.getValue(CommandObject.class);
if ("TAKE_OFF".equals(command.text)) {
mMiniDrone.takeOff();
} else if ("LAND".equals(command.text)) {
mMiniDrone.land();
}
}
}
Alex posted the right solution for me, but since I was using firebase-ui and kotlin, the code used is different. More info here
val options = FirebaseRecyclerOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage::class.java)
.build()
// equivalent options object with manual parsing, use it to debug which field gives error
val options = FirebaseRecyclerOptions.Builder<ChatMessage>()
.setQuery(query, object : SnapshotParser<ChatMessage> {
override fun parseSnapshot(snapshot: DataSnapshot): ChatMessage {
val senderId = snapshot.child("senderId").getValue(String::class.java)
val receiverId = snapshot.child("receiverId").getValue(String::class.java)
val senderName = snapshot.child("senderName").getValue(String::class.java)
val text = snapshot.child("text").getValue(String::class.java)
val timestamp = snapshot.child("timestamp").getValue(Long::class.java)
return ChatMessage(senderId, receiverId, senderName, text, timestamp)
}
})
.build()
adapterMessages = object : FirebaseRecyclerAdapter<ChatMessage, ChatHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatHolder {
val rootView = LayoutInflater.from(parent.context).inflate(R.layout.chat_message_item, parent, false)
return ChatHolder(rootView)
}
override fun onBindViewHolder(holder: ChatHolder, position: Int, model: ChatMessage) {
holder.populateItem(model)
}
}