android - NPE when try to contact with saved data -
i have nullpointerexception when try saved data.i think caused wrong "open" method @ sqlhelper class.and associated wrong context.because when used activity worked,now use fragment , have error. error:
10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication e/androidruntime: caused by: java.lang.nullpointerexception 10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication e/androidruntime: @ com.example.vnvbnv.myapplication.mainlist.displaysavedlv(mainlist.java:168) 10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication e/androidruntime: @ com.example.vnvbnv.myapplication.mainlist.onactivitycreated(mainlist.java:56)
mainlist(lines caused error i'll mark "****") :
public class mainlist extends listfragment{ sqlhelper dbhelper; listview mainlist; progressbar progbar; private static string url = "https://fierce-citadel-4259.herokuapp.com/hamsters"; private static final string title = "title"; private static final string description = "description"; private static final string image = "image"; arraylist<hashmap<string,string>> jsonlist1 = new arraylist<hashmap<string, string>>(); arraylist<hashmap<string,string>> bdlist = new arraylist<hashmap<string, string>>(); public view oncreateview(layoutinflater inflater,viewgroup container,bundle savedinstancestate) { view v = inflater.inflate(r.layout.list_fragme, null); mainlist = (listview)v.findviewbyid(android.r.id.list); progbar = (progressbar) v.findviewbyid(r.id.progressbar); return v; } public void onactivitycreated(bundle savedinstancestate){ super.onactivitycreated(savedinstancestate); if (isnetworkconnected()==true) { new progresstask().execute(); } else{ displaysavedlv();***** , this! } } @override public void onlistitemclick(listview l, view view, int position, long id) { super.onlistitemclick(l, view, position, id); string title = jsonlist1.get(position).get("title"); string description= jsonlist1.get(position).get("description"); string image = jsonlist1.get(position).get("image"); mydetailfragment detailfragment = new mydetailfragment(); bundle bundle = new bundle(); if(isnetworkconnected()==true) { bundle.putstring("title", title); bundle.putstring("description", description); bundle.putstring("image", image); } else { string dbtitle= bdlist.get(position).get("title"); string dbdescription = bdlist.get(position).get("description"); string dvimage = bdlist.get(position).get("image"); bundle.putstring("title",dbtitle); bundle.putstring("description",dbdescription); bundle.putstring("image",dvimage); } detailfragment.setarguments(bundle); fragmenttransaction fragmenttransaction = getactivity().getfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.fragmentcont,detailfragment); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); } private class progresstask extends asynctask<string,void,boolean> { private progressdialog dialog; private activity activity; private mainactivity context; private string[] params; public progresstask(mainactivity activity) throws sqlexception { this.activity = getactivity(); context = activity; dialog = new progressdialog(getactivity().getapplicationcontext()); dbhelper = new sqlhelper(getactivity().getapplicationcontext()); } public progresstask() { dialog = new progressdialog(getactivity().getapplicationcontext()); try { dbhelper = new sqlhelper(getactivity().getapplicationcontext()); } catch (sqlexception e) { e.printstacktrace(); } } @override protected boolean doinbackground(string... params) { this.params = params; jsonparser jparser = new jsonparser(); jsonarray json = jparser.getjsonfromurl(url); for(int =0;i<json.length();i++) { try { jsonobject c = json.getjsonobject(i); string vtitle = c.getstring(title); string vdescription = c.getstring(description); string vimage = c.getstring(image); dbhelper.open(); dbhelper.createentry(vtitle, vimage, vdescription); dbhelper.close(); hashmap<string, string> map = new hashmap<>(); map.put(title, vtitle); map.put(description, vdescription); map.put(image, vimage); jsonlist1.add(map); } catch (jsonexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } } return null; } protected void onpreexecute(){ progbar.setvisibility(view.visible); } protected void onpostexecute(final boolean success){ if (isnetworkconnected()==true) { progbar.setvisibility(view.gone); customlistadapter adapter = new customlistadapter(getactivity(), jsonlist1, r.layout.list_item, new string[]{title, description}, new int[]{r.id.title, r.id.description}); mainlist.setadapter(adapter); } else { displaysavedlv(); } } } private void displaysavedlv() { bdlist = dbhelper.getalldata();****(this one) customlistadapter adapter1 = new customlistadapter(getactivity(), bdlist, r.id.list_item, new string[]{title, description}, new int[]{r.id.title, r.id.description}); mainlist.setadapter(adapter1); } private boolean isnetworkconnected() { connectivitymanager cm = (connectivitymanager)getactivity().getsystemservice(context.connectivity_service); networkinfo ni = cm.getactivenetworkinfo(); if (ni == null) { // there no active networks. return false; } else return true; } }
and open method @ sqlhelper , constructor(i think problem here):
public sqlhelper(context c) throws sqlexception { ourcontext = c; try { open(); } catch (sqlexception e) { e.printstacktrace(); } } public sqlhelper open() throws sqlexception{ ourhelper = new dbhelper(ourcontext); ourdatabase = ourhelper.getwritabledatabase(); return this; }
Comments
Post a Comment