在HashMap实现中获取Null对象

时间:2018-03-30 23:20:06

标签: java android

我试图从HashMap中获取值,但是当我调用他和setText时,我总是得到Null的值,让我显示代码:

 MyValues.class

    private List<ItemsBean> items;

    public List<ItemsBean> getItems() { return items;}

    public void setItems(List<ItemsBean> items) { this.items = items; }

   public static class ItemsBean {


    private Map<String, leakBean> Gitt;

    public Map<String, leakBean> getGitt() { return Gitt;}

    public void setGitt(Map<String, leakBean> Gitt) { this.Gitt = Gitt;}

    public static class leakBean {

    private int id;
    private String dev;

    public int getId() {return id; }

    public String getDev(){return dev;}

    public void setId(int id) { this.id = id;}

    public void setDev(String dev){this.dev = dev;}
 }

我正在使用Gson来获取值并将其用于.setText或Toast我试图像这样访问:

MyValues object;

txt1.setText(String.valueOf( object.getItems().get(0).getGitt().get("id")));

在这里我得到null,有人可以帮忙吗?我只需要访问值,还有items.size();返回1并且必须返回3

这是hte JSON:

{
"id": 1001,
"name": "Super1",
"user": {
    "name": "The Super 1"
},
"items": [
    {
        "987987M7812b163eryrt": {
            "id": 1,
            "dev": "seed"
        },
        "90812bn120893juuh": {
            "id": 2,
            "dev": "none"
        },
        "981273jn19203nj123rg": {
            "id": 3,
            "dev": "mine"
        }
    }
]}

1 个答案:

答案 0 :(得分:0)

尝试使用这些POJO:

<强> MyValues.java     公共类MyValues {

    private int id;

    private String name;

    private User user;

    private List<HashMap<String, ItemsBean>> items;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<HashMap<String, ItemsBean>> getItems() {
        return items;
    }

    public void setItems(List<HashMap<String, ItemsBean>> items) {
        this.items = items;
    }
}

<强> User.java     公共类用户{

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

<强> ItemsBean.java     公共类ItemsBean {

    private int id;
    private String dev;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDev() {
        return dev;
    }

    public void setDev(String dev) {
        this.dev = dev;
    }
}

你不能像这样访问这些值:

object.getItems().get(0).get("987987M7812b163eryrt").getId();