java - Not creating arraylist Object Class from JSON in fragment (Android) -


i have fragment class showed when element selected in navigation drawer:

public class recibidosfragment extends fragment {      public expandlistadaptermensaje expadapter;     public arraylist<childmensaje> listchild=null;     public arraylist<mensaje> listmensajes=null;     public expandablelistview expandlist;     public progressdialog dialog;     public arraylist<arraylist<childmensaje>> listchildxxxxxxxxx = null;     textview tv;     context mcontext;     /**      * returns new instance of fragment given section number.      */     public static recibidosfragment newinstance() {         recibidosfragment fragment = new recibidosfragment();         return fragment;     }         public recibidosfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_recibidos, container,                 false);          mcontext = container.getcontext();          tv = (textview)rootview.findviewbyid(r.id.textview);         tv.settext(cargartodo());         expandlist=(expandablelistview)rootview.findviewbyid(r.id.expandablelistview2);          listmensajes = new arraylist<mensaje>();         listchild = new arraylist<childmensaje>();         listchildxxxxxxxxx = new arraylist<arraylist<childmensaje>>();           return rootview;     }      @override     public void onattach(activity activity) {         super.onattach(activity);         ((menu_mensajes) activity).onsectionattached(1);     }     public string cargartodo(){         string arreglo = loadjsonfromfile();         arreglo = "[" + arreglo+ "]";         mensaje men =null;         childmensaje chmen=null;         try {             arraylist<mensaje> list = new arraylist<mensaje>();             arraylist<childmensaje> ch_list;               jsonarray jsonarray = new jsonarray(arreglo);               jsonobject jsonobject2 = jsonarray.getjsonobject(0);                  if(jsonobject2.optstring("success").equals("0")){                     arreglo="no tiene mensajes recibidos.";                   }else {                     (int = 0; < jsonarray.length(); i++) {                         arreglo="inside5";                          jsonobject jsonobject = jsonarray.getjsonobject(i);                          men = new mensaje(jsonobject.optstring("asunto"), jsonobject.optstring("origen"), jsonobject.optstring("fecha"));                          listmensajes.add(men);                          chmen = new childmensaje(jsonobject.optstring("mensaje"));                          listchild.add(chmen);                          listchildxxxxxxxxx.add(listchild);                          expadapter = new expandlistadaptermensaje(mcontext, listmensajes, listchildxxxxxxxxx);                          expandlist.setadapter(expadapter);                      }}          } catch (jsonexception e) {             e.printstacktrace();         }         return arreglo;     }     public string loadjsonfromfile() {          string json = null;         try { //            fileinputstream = mcontext.openfileinput("mensajes.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;      }  } 

with :

public class childmensaje {          public string mensaje;          public childmensaje(){}         public childmensaje(string mensaje){             this.mensaje=mensaje;         }           public string getmensaje() {             return mensaje;         }          public void setmensaje(string mensaje) {             this.mensaje = mensaje;         }  }   public class expandlistadaptermensaje extends baseexpandablelistadapter {     public context context;     public arraylist<mensaje> noticiasarraylist;     public arraylist<arraylist<childmensaje>> listchild;      public expandlistadaptermensaje(context context, arraylist<mensaje> noticiasarraylist,arraylist<arraylist<childmensaje>> listchild) {         this.context = context;         this.noticiasarraylist = noticiasarraylist;         this.listchild=listchild;     }        @override     public childmensaje getchild(int groupposition, int childposition) {         return listchild.get(groupposition).get(childposition);     }      @override     public long getchildid(int groupposition, int childposition) {         return childposition;     }      @override     public view getchildview(int groupposition, int childposition,                              boolean islastchild, view convertview, viewgroup parent) {          childmensaje child = getchild(groupposition, childposition);         viewholder holder=null;         if (convertview == null) {             layoutinflater infalinflater = (layoutinflater) context                     .getsystemservice(context.layout_inflater_service);             convertview = infalinflater.inflate(r.layout.child_row, null);             holder=new viewholder();             holder.mensaje=(textview)convertview.findviewbyid(r.id.mensaje);             convertview.settag(holder);         }         else {             holder=(viewholder)convertview.gettag();         }           holder.mensaje.settext(child.getmensaje());           return convertview;     }      @override     public int getchildrencount(int groupposition) {         return listchild.get(groupposition).size();     }      @override     public mensaje getgroup(int groupposition) {         return noticiasarraylist.get(groupposition);     }      @override     public int getgroupcount() {         return noticiasarraylist.size();     }      @override     public long getgroupid(int groupposition) {         return groupposition;     }      @override     public view getgroupview(int groupposition, boolean isexpanded,                              view convertview, viewgroup parent) {         mensaje not = (mensaje) getgroup(groupposition);         viewholder holder= null;          if (convertview == null) {             layoutinflater inf = (layoutinflater) context                     .getsystemservice(context.layout_inflater_service);             convertview = inf.inflate(r.layout.list_row, null);              holder=new viewholder();             holder.asunto=(textview)convertview.findviewbyid(r.id.titulo);             holder.fecha=(textview)convertview.findviewbyid(r.id.fecha);             holder.origen=(textview)convertview.findviewbyid(r.id.dirigido);             convertview.settag(holder);          }         else{             holder=(viewholder)convertview.gettag();         }          holder.asunto.settext(not.getasunto());         holder.fecha.settext(not.getfecha());         holder.origen.settext(not.getorigen());           return convertview;     }      @override     public boolean hasstableids() {         return true;     }      @override     public boolean areallitemsenabled()     {         return true;     }      @override     public boolean ischildselectable(int groupposition, int childposition) {         return true;     }      static class viewholder{         textview asunto, fecha,origen, mensaje;     }   }  public class mensaje {      public string origen;     public string asunto;      public string getfecha() {         return fecha;     }      public void setfecha(string fecha) {         this.fecha = fecha;     }      public string getorigen() {         return origen;     }      public void setorigen(string origen) {         this.origen = origen;     }      string fecha;       arraylist<childmensaje> items;      public mensaje() {     }      public mensaje(string asunto, string origen, string fecha) {         this.asunto = asunto;         this.fecha = fecha;         this.origen = origen;     }      public string getasunto() {         return asunto;     }      public void setasunto(string asunto) {         this.asunto = asunto;     }      public arraylist<childmensaje> getitems() {         return items;     }      public void setitems(arraylist<childmensaje> items) {         this.items = items;     } } 

my issue appear when i'm running app. in moment clicked button go fragment message appear, aplication must stopped , crashed. commenting code line per line , know code executed until line ->

listmensajes.add(men); 

so, i'm doing wrong ?? has solution ?? ;)

ray's comment right. seems error occurs in oncreateview() method.

tv.settext(cargartodo()); ... listmensajes = new arraylist<mensaje>(); 

cargartodo() called before listmensajes initialized. in cargartodo() method, listmensajes.add() throw nullpointerexception.

change public arraylist<mensaje> listmensajes=null public arraylist<mensaje> listmensajes= new arraylist<mensaje> , may work.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -