NullPointerException when accessing values from a HashMap

2

I'm trying to access the values of a HashMap<> but the application stops when from a custom adapter I try to take the values I passed to the HashMap , showing this message in the logcat:

  

E / AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
  at my.app.example.app.adapterstack.getView (adapterstack.java:41)

It stops at this line of code:

nombre.setText(item.getCodigo().get(i).getId());

This is my code:

Adapter:

public class adapterstack extends BaseAdapter {
    List<stack.ItemsBean> itemlist;
    Context sContext;
    public adapterstack(Context sContext, List<stack.ItemsBean> itemlist) {
        this.sContext = sContext;
        this.itemlist = itemlist;}
    @Override
    public int getCount() {return itemlist.size();}

    @Override
    public Object getItem(int i) {return itemlist.get(i);}

    @Override
    public long getItemId(int i) {return i;}

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) sContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rootView = inflater.inflate(R.layout.item_act, viewGroup, false);
        final stack.ItemsBean item = (stack.ItemsBean) getItem(i);
        TextView nombre = (TextView) rootView.findViewById(R.id.txtname);
        TextView genero = (TextView) rootView.findViewById(R.id.txtid);

        //TODO: En esta linea de código la app se detiene.

        nombre.setText(item.getCodigo().get(i).getId());
        genero.setText(item.getCodigo().get(i).getStrong());
        return rootView;
    }
}

Class where the JSON passed to Java:

public class stack {
    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, CodigoBean> codigo = new HashMap<String,CodigoBean>();
        public HashMap<String, CodigoBean> getCodigo(){return codigo;}
        public void setCodigo(HashMap<String, CodigoBean> codigo) {this.codigo = codigo;}

        public static class CodigoBean {
            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;}
        }
    }
}

The 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"
    }
  }]
}
    
asked by LuDev 11.03.2018 в 07:57
source

0 answers