Team i need a right way to store data into a HashMap<>
using Gson coz i´m doing as the follow Java class but when i try to get some value from the HashMap<>
i just get NullPointerException
Error, i succesfull get the Json to Java class coz when i try to get some value out of the Hashmap<>
, Why i need to store it in HashMap<>
thats coz the Objects names are very random and i can´t make a class for each item.
Java class where i try to store the JSON data:
public class Response {
private int id;
private String name;
private UserBean user;
private List<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 UserBean getUser() {return user;}
public void setUser(UserBean user) {this.user = user;}
public List<ItemsBean> getItems() {return items;}
public void setItems(List<ItemsBean> items) {this.items = items;}
public static class UserBean {
private String name;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
public static class ItemsBean {
private HashMap<String, CodecBean> codec;
public HashMap<String, CodecBean> getCodec() {return codec;}
public void setCodec(HashMap<String, CodecBean> codec) {this.codec = codec;}
public static class CodecBean {
private int id;
private int strong;
private boolean active;
private String sell;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public int getStrong() {return strong;}
public void setStrong(int strong) {this.strong = strong;}
public boolean isActive() {return active;}
public void setActive(boolean active) {this.active = active;}
public String getSell() {return sell;}
public void setSell(String sell) {this.sell = sell;}
}
}}
JSON:
{
"id": 1001,
"name": "Super1",
"user": {
"name": "The Super 1"
},
"items": [{
"987987M7812b163eryrt": {
"id": 1,
"strong": 456,
"active": true,
"sell": "te"
},
"90812bn120893juuh": {
"id": 2,
"strong": 4700,
"active": true,
"sell": "tt"
},
"981273jn19203nj123rg": {
"id": 3,
"strong": 3000,
"active": true,
"sell": "ti"
}
}]
}
I try to get values as this way:
Response.ItemsBean item = (Response.ItemsBean) getItem(i);
TextView txt;
txt.setText(item.getCodec().get(i).getSell());
Can you please tell me what i'm doing wrong please?
Thanks in advance