Retrieving data from the database
I always get below stacktrace error every time I implement
GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {
};
GenericTypeIndicator<List<String>> t2 = new GenericTypeIndicator<List<String>>() {};
List<String> namelist = snapshot.child("Driver name").getValue(t);
List<String> carlist = snapshot.child("Drivers car").getValue(t2);
if (namelist!=null & carlist!=null){
for(int wee=0;wee<namelist.size();wee++){
driver.add(new Driver(namelist.get(wee).toString(),carlist.get(wee).toString()));
}
The data I sent to firebase is stored as:
"DriversList" : {
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
},
"Drivers availability" : {
"-Js6OJozSuUvj5JnuNnW" : "No",
"-Js7GjQ_5YgmnEYKWHrL" : "No",
"-Js7GlVm7onoQd59Yyfq" : "No",
"-Js7GnDPdjseoRVjgwSn" : "No",
"-Js7GqIYy4VSEqu9qZfC" : "No",
"-Js7Gs8VP5mp5bOR1pxR" : "No",
"-Js7Gtmfv2M2p201k3Ra" : "No",
"-Js7Gyvf8JkGwBlh8ZLu" : "No",
"-JsPxEly9iGncA4VWvn2" : "No",
"-JsQBoOE6AbGy1VMBSWp" : "No",
"-JsQC6wGzBXBMz76IXue" : "No"
},
"Drivers car" : {
"-Js6OJoyDlc4DR0IQhJr" : "gvgh",
"-Js7GjQZ6Sb7AOooABt3" : "nbcgg",
"-Js7GlVajAA1B5PeBnn2" : "nvvuuu",
"-Js7GnDOCiWM874f6iL6" : "",
"-Js7GqIYy4VSEqu9qZfB" : "nbcxx",
"-Js7Gs8VP5mp5bOR1pxQ" : "hbccf",
"-Js7Gtmfv2M2p201k3R_" : "hvccf",
"-Js7Gyvf8JkGwBlh8ZLt" : "gghhhg",
"-JsPxElx9XXm0kVLFstN" : "",
"-JsQBoOE6AbGy1VMBSWo" : "dd",
"-JsQC6wF0zgLApv8SwJO" : "ff"
}
Stack trace error:
06-30 14:35:44.918 2736-2736/com.kate.teme E/AndroidRuntimeοΉ FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:208)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.shaded.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@42af1900; line: 1, column: 1]
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:218)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:166)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:156)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:206)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
+3
source to share
1 answer
Looking at your data structure:
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
}
You are trying to deserialize this into List<String>
. But actually it is Map<String,String>
. So:
@Override
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,String>> mapType = new GenericTypeIndicator<Map<String, String>>() { };
Map<String,String> namelist = snapshot.child("Driver name").getValue(mapType);
Map<String,String> carlist = snapshot.child("Drivers car").getValue(mapType);
if (namelist!=null & carlist!=null) {
for (String name: namelist.values()) {
System.out.println(name);
}
}
}
Please note that I have no idea how you are trying to match the driver and their car. I highly recommend that you review this data structure before continuing with the code.
One possible structure:
"DriversList": {
"-Js6OJniEqVZAYSNhqbB" : {
"name": "puf",
"available": false,
"car": "Toyota RAV4"
},
"-Js7GjPMGNOLurrKk9rC" : {
"name": "manon",
"available": false,
"car": "Toyota Matrix"
},
...
}
To read / write them, you need a class Driver
:
public class Driver {
String name;
Boolean available;
String car;
public Driver() {}
public String getName() { return name; }
public Boolean getAvailable() { return available; }
public String getCar() { return car; }
}
And then:
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,Driver>> mapType = new GenericTypeIndicator<Map<String, Driver>>() { };
Map<String,Driver> drivers = snapshot.getValue(mapType);
if (drivers!=null) {
for (Driver driver: drivers.values()) {
System.out.println(driver.name);
}
}
}
+6
source to share