V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Amarok
V2EX  ›  Android

Gson 报空指针...死活搞不明白怎么回事,求指点~~

  •  
  •   Amarok · 2016-12-21 11:26:01 +08:00 · 9249 次点击
    这是一个创建于 2894 天前的主题,其中的信息可能已经有所发展或是发生改变。
    20 条回复    2016-12-21 13:30:46 +08:00
    Amarok
        1
    Amarok  
    OP
       2016-12-21 11:27:07 +08:00
    String jsonText = jsonStringTool.Optimization(s);
    Log.e(TAG, jsonText);


    Gson detailGson = new Gson();

    PostingExample postingExample = detailGson.fromJson(jsonText,PostingExample.class);
    String id = postingExample.getPostings().get(1).getID();
    String pID = postingExample.getPostings().get(1).getPid();

    String Contents = postingExample.getPostings().get(1).getContents();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", id);
    map.put("pID", pID);


    map.put("Contents", Contents);

    log 出来的 json 字符串没问题...到操作 get ( 1 )的时候就报空指针...
    Amarok
        2
    Amarok  
    OP
       2016-12-21 11:31:56 +08:00
    getPostings().size()就报空指针...T.T
    hinkal
        3
    hinkal  
       2016-12-21 11:33:19 +08:00 via Android
    你这个代码没贴全, PostingExample 的类结构呢
    xuhaoyangx
        4
    xuhaoyangx  
       2016-12-21 11:33:20 +08:00
    只给代码不给错误信息,而且你这个代码也不全,只说空指针,可以
    Amarok
        5
    Amarok  
    OP
       2016-12-21 11:36:38 +08:00
    还没发完就被老板叫过去了....回来补上代码....

    PostingExample.class
    public class PostingExample {
    private List<PostingDetail> Postings;

    public List<PostingDetail> getPostings() {
    return Postings;
    }

    public void setPostings(List<PostingDetail> postings) {
    Postings = postings;
    }

    }


    PostingDetail .class
    public class PostingDetail {
    private String FocusName;
    private String status;
    private String sportsName;
    private String ptype;
    private String UserName;
    private String ID;
    private String postType;
    private String baseTitle;
    private String fixedTitle;
    private String sportstypeName;
    private String collection;
    private String sendTime;
    private String pid;
    private String titleimg;
    private String contents;

    public String getID() {
    return ID;
    }

    public void setID(String ID) {
    this.ID = ID;
    }

    public String getBaseTitle() {
    return baseTitle;
    }

    public void setBaseTitle(String baseTitle) {
    this.baseTitle = baseTitle;
    }

    public String getFixedTitle() {
    return fixedTitle;
    }

    public void setFixedTitle(String fixedTitle) {
    this.fixedTitle = fixedTitle;
    }

    public String getCollection() {
    return collection;
    }

    public void setCollection(String collection) {
    this.collection = collection;
    }

    public String getSendTime() {
    return sendTime;
    }

    public void setSendTime(String sendTime) {
    this.sendTime = sendTime;
    }

    public String getStatus() {
    return status;
    }

    public void setStatus(String status) {
    this.status = status;
    }

    public String getPid() {
    return pid;
    }

    public void setPid(String pid) {
    this.pid = pid;
    }

    public String getTitleimg() {
    return titleimg;
    }

    public void setTitleimg(String titleimg) {
    this.titleimg = titleimg;
    }

    public String getFocusName() {
    return FocusName;
    }

    public void setFocusName(String focusName) {
    FocusName = focusName;
    }

    public String getSportstypeName() {
    return sportstypeName;
    }

    public void setSportstypeName(String sportstypeName) {
    this.sportstypeName = sportstypeName;
    }

    public String getSportsName() {
    return sportsName;
    }

    public void setSportsName(String sportsName) {
    this.sportsName = sportsName;
    }

    public String getPtype() {
    return ptype;
    }

    public void setPtype(String ptype) {
    this.ptype = ptype;
    }

    public String getUserName() {
    return UserName;
    }
    Amarok
        6
    Amarok  
    OP
       2016-12-21 11:38:04 +08:00
    jsonText :
    {
    "Posting": [
    {
    "UserName": "小袁",
    "FocusName": "衰神",
    "status": "未审核",
    "sportsName": "网球",
    "ptype": "原创",
    "ID": "11",
    "sportstypeName": "竞技球类",
    "baseTitle": "小米手环 2 开箱及与小米手环 1 对比",
    "fixedTitle": "小米哈哈 666",
    "seen": "900",
    "collection": "170",
    "sendTime": "08 27 2016 11:11AM",
    "pid": "20160827111111",
    "titleimg": "http://imageqiniu.xxxxxbbs.com/FrtK2K9JqwoE6xhYWRPFv9tIZym4?watermark/1/image/aHR0cDovLzd4c2N3NS5jb20wLnowLmdsYi5xaW5pdWNkbi5jb20vd2F0ZXJtYXJrLnBuZw==/gravity/South/dy/5",
    "Contents": "aaa"
    }
    ]
    }
    Amarok
        7
    Amarok  
    OP
       2016-12-21 11:39:06 +08:00
    @hinkal 贴上了....谢谢~
    CFO
        8
    CFO  
       2016-12-21 11:39:18 +08:00 via Android
    实体类还是用 GsonFormat 生成吧 靠谱
    Amarok
        9
    Amarok  
    OP
       2016-12-21 11:40:00 +08:00
    @xuhaoyangx 太着急了刚刚,已经贴上了.....谢谢~
    sjn9588
        10
    sjn9588  
       2016-12-21 11:40:15 +08:00
    postingExample.getPostings()返回的 Postings 是 null 吧?
    Ouyangan
        11
    Ouyangan  
       2016-12-21 11:40:18 +08:00
    我想说你为什么不把反序列化出来的对象打印出来呢?
    shakespaces
        12
    shakespaces  
       2016-12-21 11:43:28 +08:00
    你这个类方法没看到和 content 相关的东西啊
    sorra
        13
    sorra  
       2016-12-21 11:44:49 +08:00
    一个叫 Posting ,一个叫 Postings ,不匹配啊
    xuhaoyangx
        14
    xuhaoyangx  
       2016-12-21 11:45:06 +08:00
    我怎么感觉的你的 json 有问题呢。。
    xuhaoyangx
        15
    xuhaoyangx  
       2016-12-21 11:46:30 +08:00   ❤️ 2
    private List<PostingDetail> Postings
    但是你的 json 那个数组名字是 Posting
    Amarok
        16
    Amarok  
    OP
       2016-12-21 11:47:08 +08:00
    @sjn9588 对...是 null
    sjn9588
        17
    sjn9588  
       2016-12-21 11:48:57 +08:00   ❤️ 1
    @Amarok 15 楼正解
    Amarok
        18
    Amarok  
    OP
       2016-12-21 11:49:41 +08:00
    @xuhaoyangx 啊啊啊啊啊我瞎了.....这么低级的问题,捂脸......
    pubby
        19
    pubby  
       2016-12-21 12:32:57 +08:00
    @SerializedName("Posting")
    private List<PostingDetail> Postings;

    名字不一致没关系,上面加个注解也能解决
    wanttofly
        20
    wanttofly  
       2016-12-21 13:30:46 +08:00
    话说不用 GsonFormat 吗?应该不会出现变量名这个问题了啊。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1075 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 18:49 · PVG 02:49 · LAX 10:49 · JFK 13:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.