Sunday, 15 January 2012

android - Getting NullPointerException while parse JSON using GSON -



android - Getting NullPointerException while parse JSON using GSON -

i seek parse json using gson library. ref. link have used : link

json file:

{"details": [{"enddate": "01/11/2013 05:10:00","type": "numeric", "startdate": "12/12/2012 04:00:00", "utcdate": "12/31/2012 07:55:01", "min": "1000.00", "convertedstartdate": "12/12/2012 04:00:00", "convertedutcdate": "12/31/2012 07:55:01", "name": "user auto auction", "max": "3000.00", "convertedenddate": "01/11/2013 05:10:00" }], "result": [{ "allowtocomment": false, "rank": 1, "low": "5000.00", "detailsid": 51, "itemdetails": {"id": 28, "specificationfile": "", "itemdescription": "pristine white accord. 176k miles. runs great", "imagename": "http://64.151.125.199/auction/28/desert.jpg", "itemname": "1986 honda accord"}, "desc": "pristine white accord. 176k miles. runs great", "comment": "<null>", "detailsid": 78, "name": "1986 honda accord", "lowrank": 1 }, { "allowtocomment": false, "rank": 1, "low": "7000.00", "detailsid": 52, "itemdetails": {"id": 28, "specificationfile": "", "itemdescription": "black w/tan interior. 110k miles. new tires", "imagename": "http://64.151.125.199/auction/28/penguins.jpg", "itemname": "1989 isuzu trooper"}, "desc": "black w/tan interior. 110k miles. new tires", "comment": "n/a", "detailsid": 100, "name": "1989 isuzu trooper", "lowrank": 1 }]}

itemdetails.java

public class itemdetails { public int id; public string specificationfile; public string itemdescription; public string imagename; public string itemname; }

result.java

public class result { public boolean allowtocomment; public int rank; public string low; public int detailsid; public boolean showcompetingbid; public itemdetails itemdetails; public string desc; public string comment; public int detailsid; public string name; public int lowrank; }

detail.java

public class detail { public string enddate; public string type; public string startdate; public string utcdate; public string min; public string convertedstartdate; public string convertedutcdate; public string name; public string max; public string convertedenddate; }

responce.java

public class responce { public list<detail> details; public list<result> results; }

main java code

public class gsonparsingactivity extends activity { private progressbar bgprocess; private string server = "url"; private string tag = "largejson"; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_gson); bgprocess = (progressbar) findviewbyid(r.id.progressbargson); new getdetail().execute(); } private class getdetail extends asynctask<string, string, string> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); bgprocess.setvisibility(view.visible); } @override protected string doinbackground(string... params) { // todo auto-generated method stub seek { map<string, string> mmap = new hashmap<string, string>(); mmap.put("id", "28"); mmap.put("participantid", "2"); mmap.put("companyid", "5"); inputstream source = getdataforsimplemap(server + "getlist", mmap); gson gson = new gson(); reader reader = new inputstreamreader(source); auctionresponce responce = gson.fromjson(reader, responce.class); list<result> results = responce.results; log.d(tag, ">>>>>>>>>>>>>> " + results.size()); (result result : results) { log.v(tag, " ==================> "+ result.biddetailsid); } } grab (exception e) { e.printstacktrace(); } homecoming null; } @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); bgprocess.setvisibility(view.invisible); } } @suppresswarnings("rawtypes") private inputstream getdataforsimplemap(string strurl, map<string, string> params) { seek { // jsonobject jarray = null; inputstream stream = null; iterator iterator = params.entryset().iterator(); jsonobject info = new jsonobject(); while (iterator.hasnext()) { map.entry mentry = (map.entry) iterator.next(); string key = mentry.getkey().tostring(); string value = mentry.getvalue().tostring(); data.put(key, value); } stream = getjsonfromurl(strurl, data); homecoming stream; } grab (exception e) { e.printstacktrace(); homecoming null; } } private inputstream getjsonfromurl(string strurl, jsonobject data) { seek { inputstream stream = null; httpclient httpclient = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(httpclient.getparams(), 120000); httppost httppost = new httppost(strurl); httppost.setheader("content-type", "application/json"); if (data != null) { stringentity entity = new stringentity(data.tostring(), http.utf_8); httppost.setentity(entity); } httpresponse response = null; response = httpclient.execute(httppost); if (response != null) { stream = response.getentity().getcontent(); } else { stream = null; } homecoming stream; } grab (exception e) { e.printstacktrace(); homecoming null; } } }

but unfortunate getting next error:

02-11 12:10:01.075: w/system.err(587): java.lang.nullpointerexception 02-11 12:10:01.094: w/system.err(587): @ com.example.largejson.gsonparsingactivity$getauctiondetail.doinbackground(gsonparsingactivity.java:80) 02-11 12:10:01.094: w/system.err(587): @ com.example.largejson.gsonparsingactivity$getauctiondetail.doinbackground(gsonparsingactivity.java:1) 02-11 12:10:01.094: w/system.err(587): @ android.os.asynctask$2.call(asynctask.java:185) 02-11 12:10:01.094: w/system.err(587): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 02-11 12:10:01.105: w/system.err(587): @ java.util.concurrent.futuretask.run(futuretask.java:138) 02-11 12:10:01.105: w/system.err(587): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 02-11 12:10:01.115: w/system.err(587): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 02-11 12:10:01.115: w/system.err(587): @ java.lang.thread.run(thread.java:1019)

android json gson

No comments:

Post a Comment