NullPointerException异常反序列化与GSON [英] NullPointerException deserializing with gson

查看:169
本文介绍了NullPointerException异常反序列化与GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的JSON:

<$p$p><$c$c>{\"p1\":{\"debris\":{\"titanium\":0,\"silicum\":0}},\"p2\":{\"debris\":{\"titanium\":0,\"silicum\":0}}...

我宣布使用HashMap中的数据:

 公共类加拉克斯{    公众的HashMap&LT;弦乐,InnerObject&GT; POS;
}公共类InnerObject {    公共碎片杂物;
}公共类碎片{    公共双钛,silicum;
}

我呼吁使用这个数据:

 加拉克斯GALAX =新GSON()fromJson(strGalaxy,Galax.class)。
        的System.out.println(galax.pos.get(P2)debris.titanium);

问题是,我的总是获取试图证明日志中的数据时,NullPointerException异常......我不明白为什么,因为我用它做完全相同的方式来获得的其他数据JSON和它完美...

可以是什么问题?

感谢你在前进!


解决方案

我看到POS在code,但不是在JSON字符串。官方的回答。 : - )

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!

解决方案

I see "pos" in the code but not in the JSON string. official answer. :-)

这篇关于NullPointerException异常反序列化与GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆