android - nullpointerexception when trying to get json array or json object from json string -
this question has answer here:
- android problems parsing json 2 answers
hello keep getting npe when try object jsonstring. tried alot of things changed json coople of times it's not working. httpresponse works fine , when log "myobject" gives right object. when try object in it gives me npe. tested json , valid. tried retrieve array instead of object gives npe. can tell me how fix this.
i made simple jsontester activity test json:
public class jsontester extends activity { private defaulthttpclient createhttpclient() { httpparams my_httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(my_httpparams, 3000); schemeregistry registry = new schemeregistry(); registry.register(new scheme("http", plainsocketfactory.getsocketfactory(), 80)); threadsafeclientconnmanager multithreadedconnectionmanager = new threadsafeclientconnmanager(my_httpparams, registry); defaulthttpclient httpclient = new defaulthttpclient(multithreadedconnectionmanager, my_httpparams); return httpclient; } mikeyjson mjson; button mbtngo; textview mtxt1; textview mtxt2; textview mtxt3; textview mtxt4; progressdialog mprogressdialog; private string lang; private int length; // 0 - 6 (length 7 length 12) private int wordpos; // 0 - array length @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_jsontester); mtxt1 = (textview) findviewbyid(r.id.txt1); mtxt1 = (textview) findviewbyid(r.id.txt1); mtxt1 = (textview) findviewbyid(r.id.txt1); mtxt1 = (textview) findviewbyid(r.id.txt1); mbtngo = (button) findviewbyid(r.id.btngo); } public boolean isnumeric(string str) { for(int i=0;i<str.length();i++) { if(character.isdigit(str.charat(i))) { return true; } } return false; } public void testjson(view view) { if(view==mbtngo) { mprogressdialog = new progressdialog(this); mprogressdialog.setmessage("loading"); mprogressdialog.show(); new downloadnewwords().execute(); } } private class downloadnewwords extends asynctask<void, void, void> { int mstatuscode = 0; string mresultstring; exception mconnectionexception; @override protected void doinbackground(void... args) { string fetchurl = "http://www.mikeywebs.nl/json/jsonexample.html"; defaulthttpclient httpclient = createhttpclient(); httpget httpget = new httpget(fetchurl); try { httpresponse response = httpclient.execute(httpget); statusline statusline = response.getstatusline(); mstatuscode = statusline.getstatuscode(); if (mstatuscode == 200){ mresultstring = entityutils.tostring(response.getentity()); } } catch (clientprotocolexception e) { e.printstacktrace(); mconnectionexception = e; } catch (ioexception e) { e.printstacktrace(); mconnectionexception = e; } return null; } @override protected void onpostexecute(void arg) { mprogressdialog.dismiss(); if (mstatuscode == 200){ mjson = new mikeyjson(mresultstring); lang = "english"; //integer.parseint(langu); length = 7; //integer.parseint(wordl); wordpos = 0; string getword = mjson.getresult(lang, length, wordpos); mtxt4.settext(getword); } else { toast.maketext(jsontester.this, "gegevens konden niet worden opgehaald. controleer uw internetverbinding en probeer het opnieuw (" +mconnectionexception.tostring() + ")" , toast.length_long).show(); mjson = null; } } } }
and jsonclass use is:
public class mikeyjson { private jsonobject myobject; private jsonarray jsonarray; int i; public mikeyjson(string jsonstring) { log.i("json", "jsonstring: " + jsonstring); try { jsonobject myobject = new jsonobject(jsonstring); log.i("json", "myobject_object: " + myobject.tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } public string getresult(string lang, int length, int wordpos) { string word = "0"; //0 nederlands 1 english int la = 0; if(lang.equals("english")) { la = 1; } //make string length string le = "length" + integer.tostring(length); log.i("parse", "get_length: " + le); //the json try { jsonarray = myobject.getjsonarray("galgjejson"); log.i("json", "jsonarray: " + jsonarray.tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } return word; } }
here json:
{ "galgjejson" : [ { "nederlands" : [ { "length7" : [ { "word" : "android" }, { "word" : "camping" }, { "word" : "koekjes" } ] } ] }, { "english" : [ { "length7" : [ { "word" : "android" }, { "word" : "camping" }, { "word" : "koekjes" } ] } ] } ] }
and here log:
03-18 14:06:23.178: i/json(6719): myobject_object: {"nederlands":[{"length7": [{"word":"android"},{"word":"camping"},{"word":"koekjes"}]}]} 03-18 14:06:23.178: i/parse(6719): get_length: length7 03-18 14:06:23.178: d/androidruntime(6719): shutting down vm 03-18 14:06:23.178: w/dalvikvm(6719): threadid=1: thread exiting uncaught exception (group=0x40a13300) 03-18 14:06:23.207: e/androidruntime(6719): fatal exception: main 03-18 14:06:23.207: e/androidruntime(6719): java.lang.nullpointerexception 03-18 14:06:23.207: e/androidruntime(6719): @ me.mikey.my.games.galgjex.mikeyjson.<init>(mikeyjson.java:38) 03-18 14:06:23.207: e/androidruntime(6719): @ me.mikey.my.games.galgjex.jsontester$downloadnewwords.onpostexecute(jsontester.java:128) 03-18 14:06:23.207: e/androidruntime(6719): @ me.mikey.my.games.galgjex.jsontester$downloadnewwords.onpostexecute(jsontester.java:1) 03-18 14:06:23.207: e/androidruntime(6719): @ android.os.asynctask.finish(asynctask.java:631) 03-18 14:06:23.207: e/androidruntime(6719): @ android.os.asynctask.access$600(asynctask.java:177) 03-18 14:06:23.207: e/androidruntime(6719): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 03-18 14:06:23.207: e/androidruntime(6719): @ android.os.handler.dispatchmessage(handler.java:99) 03-18 14:06:23.207: e/androidruntime(6719): @ android.os.looper.loop(looper.java:137) 03-18 14:06:23.207: e/androidruntime(6719): @ android.app.activitythread.main(activitythread.java:4745) 03-18 14:06:23.207: e/androidruntime(6719): @ java.lang.reflect.method.invokenative(native method) 03-18 14:06:23.207: e/androidruntime(6719): @ java.lang.reflect.method.invoke(method.java:511) 03-18 14:06:23.207: e/androidruntime(6719): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 03-18 14:06:23.207: e/androidruntime(6719): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-18 14:06:23.207: e/androidruntime(6719): @ dalvik.system.nativestart.main(native method)
oh , line 38 is:
jsonarray = myobject.getjsonarray("galgjejson");
myobject
null since have in class , privately within mikeyjson. should try:
private jsonobject myobject; private jsonarray jsonarray; int i; public mikeyjson(string jsonstring) { log.i("json", "jsonstring: " + jsonstring); try { myobject = new jsonobject(jsonstring); log.i("json", "myobject_object: " + myobject.tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } }
looking @ json pretty sure can create jsonarray using string in constructor instead of jsonobject.
edited example.
private jsonarray jsonarray; int i; public mikeyjson(string jsonstring) { try { jsonarray = new jsonarray(jsonstring); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } }
Comments
Post a Comment