java - Android - JSON array from asset and NullPointerException -


i want make app takes data local json file , puts custom expandablelistview. here code:

parent class:

public class parent {      private string name;     private arraylist<childdata> child;      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public arraylist<childdata> getchild() {         return child;     }      public void setchild(arraylist<childdata> child) {         this.child = child;     } } 

childdata class:

public class childdata {     private string opis1;     private string opis2;     private string img_id;      public string getopis1() {         return opis1;     }      public void setopis1(string opis1) {         this.opis1 = opis1;     }      public string getopis2() {         return opis2;     }      public void setopis2(string opis2) {         this.opis2 = opis2;     }      public string getimg_id() {         return img_id;     }      public void setimg_id(string img_id) {         this.img_id = img_id;     } } 

mainactivity class:

public class mainactivity extends expandablelistactivity {      private arraylist<parent> parents;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          getexpandablelistview().setgroupindicator(null);         getexpandablelistview().setdividerheight(1);         registerforcontextmenu(getexpandablelistview());          //creating static data in arraylist         final arraylist<parent> datalist = putdataintoarrays();          // adding arraylist data expandablelistview values         loadhosts(datalist);      }       public string loadjsonfromasset() {         string json = null;         try {              inputstream = getassets().open("data.json");              int size = is.available();              byte[] buffer = new byte[size];              is.read(buffer);              is.close();              json = new string(buffer, "utf-8");           } catch (ioexception ex) {             ex.printstacktrace();             return null;         }         return json;      }      public arraylist<parent> putdataintoarrays() {         jsonobject obj = null;          arraylist<parent> list = new arraylist<>();         parent parent = new parent();         childdata cd = new childdata();          try {             obj = new jsonobject(loadjsonfromasset());         } catch (jsonexception e) {             e.printstacktrace();         }         try {             jsonarray m_jarry = obj.getjsonarray("data");              (int = 0; < m_jarry.length(); i++) {                  jsonobject jo_inside = m_jarry.getjsonobject(i);                  parent.setname(jo_inside.getstring("title"));                 log.d("test", "parent name: " + parent.getname());                  cd.setopis1(jo_inside.getstring("desc"));                 cd.setopis2(jo_inside.getstring("desc2"));                 cd.setimg_id(jo_inside.getstring("img"));                 log.d("test", "o1: " + cd.getopis1());                 log.d("test", "o2: " + cd.getopis2());                 log.d("test", "img: " + cd.getimg_id());                  parent.getchild().add(cd);                  log.d("test", "parent: " + parent.getchild());                  list.add(parent);                 log.d("test", "list: " + list.get(i));             }         } catch (jsonexception e) {             e.printstacktrace();         }         return list;     }      private void loadhosts(final arraylist<parent> newparents)     {         if (newparents == null)             return;          parents = newparents;          // check expandablelistadapter object         if (this.getexpandablelistadapter() == null)         {             //create expandablelistadapter object             final expandablelistadapter madapter = new expandablelistadapter();              // set adapter expandablelist adapter             this.setlistadapter(madapter);         }         else         {             // refresh expandablelistview data             ((expandablelistadapter)getexpandablelistadapter()).notifydatasetchanged();         }     } } 

and data.json

    { "data": [ {     "title": "t1",     "desc": "d1",     "desc2": "opis1",     "img": "1" }, {     "title": "t2",     "desc": "d2",     "desc2": "opis2",     "img": "2" }, {     "title": "t3",     "desc": "d3",     "desc2": "opis3",     "img": "3" }, {     "title": "t4",     "desc": "d4",     "desc2": "opis4",     "img": "1" }     ] } 

also when run app, i'm getting no results logs:

log.d("test", "rodzic1: " + parent.getchild()); log.d("test", "list: " + list.get(i)); 

you didn't initialize arraylist in parent class.

private arraylist<childdata> child; 

in parent constructor, should add:

child = new arraylist<childdata>(); 

i guessing nullpointerexception coming from.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -