问题
I have a JSON that contains the following:
{"p1":{"debris":{"titanium":0,"silicum":0}},"p2":{"debris":{"titanium":0,"silicum":0}}...
I declare the data using HashMap:
public class Galax {
public HashMap <String, InnerObject> pos;
}
public class InnerObject {
public Debris debris;
}
public class Debris {
public double titanium, silicum;
}
And I call the data using this:
Galax galax = new Gson().fromJson(strGalaxy, Galax.class);
System.out.println(galax.pos.get("p2").debris.titanium);
The problem is that I always get NullPointerException when trying to show the data in the log... I do not understand why, because I use do it exactly the same way to get data of other JSON and it works perfectly...
What can be the problem?
Thank you in advance!
回答1:
I see "pos" in the code but not in the JSON string. official answer. :-)
来源:https://stackoverflow.com/questions/9963464/nullpointerexception-deserializing-with-gson