NullPointerException deserializing with gson

好久不见. 提交于 2019-12-24 06:40:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!