Reputation: 549
I'm trying to fetch firebase database data into a list, i get "com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String" or nullpointerexception at Products product = ds.getValue(Products.class);
public class Products {
private String id;
private String reccomended;
public Products(){}
public Products(String id, String reccomended){
this.id = id;
this.reccomended = reccomended;
}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getReccomended() {return reccomended;}
public void setReccomended(String reccomended) {this.reccomended = reccomended;}
}
Listener (locationSelected is location (currently "1" or "2") passed from another activity loaded from spinner)
dbRef.child(locationSelected).child("products").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
Products product = ds.getValue(Products.class);
list.add(product);
}
for (int i=0; i<list.size(); i++){
Log.d("LIST: ", list.get(i).toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
for example:
Log.d("LOG: ", ds.getValue().toString());
gives this output:
D/LOG:: {reccomended=7, id=502100}
D/LOG:: {reccomended=10, id=502267}
D/LOG:: {reccomended=15, id=502291}
Upvotes: 0
Views: 75
Reputation: 400
You can Override toString method as per your requirement in your model class.
public String toString(){
return "{reccomended = "+this.reccomended+ ", id =" + this.id +"}";
}
Upvotes: 1
Reputation: 6073
Try using numeric data types for numeric data:
public class Products {
private Long id;
private Integer reccomended;
//...
Seems like Firebase wants to convert your ID to long based on the content rather than String.
Upvotes: 1