php - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference -
i reading data database android. copied code , used again in other app gave me error.
logcat:
07-08 08:52:24.422: e/buffer error(2636): error converting result java.lang.nullpointerexception: lock == null 07-08 08:52:24.422: e/json parser(2636): error parsing data org.json.jsonexception: end of input @ character 0 of 07-08 08:52:24.423: d/androidruntime(2636): shutting down vm 07-08 08:52:24.424: e/androidruntime(2636): fatal exception: main 07-08 08:52:24.424: e/androidruntime(2636): process: com.example.purplerose, pid: 2636 07-08 08:52:24.424: e/androidruntime(2636): java.lang.runtimeexception: unable start activity componentinfo{com.example.purplerose/com.example.purplerose._appetizer}: java.lang.nullpointerexception: attempt invoke virtual method 'org.json.jsonarray org.json.jsonobject.getjsonarray(java.lang.string)' on null object reference php code data showing up
<?php include_once("connection.php"); $sql = "select menuid, name, description2, price menu"; $result = mysqli_query($con,$sql); $json = array(); if(mysqli_num_rows($result)){ while($row=mysqli_fetch_assoc($result)){ $json['menu'][]=$row; } } mysqli_close($con); echo json_encode($json); ?> jsonparser
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(final string url) { // making http request try { // construct client , http request. defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); // execute post request , store response locally. httpresponse httpresponse = httpclient.execute(httppost); // extract data response. httpentity httpentity = httpresponse.getentity(); // open inputstream data content. = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { // create bufferedreader parse through inputstream. bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); // declare string builder parsing. stringbuilder sb = new stringbuilder(); // declare string store json object data in string form. string line = null; // build string until null. while ((line = reader.readline()) != null) { sb.append(line + "\n"); } // close input stream. is.close(); // convert string builder data actual string. json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json object. return jobj; } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; }} java code
listview mlistview = (listview) findviewbyid(r.id.listview1); string url = "http://10.0.2.2/purple/readmenu.php"; try { jsonparser jparser = new jsonparser(); jsonobject table = jparser.getjsonfromurl(url); jsonarray data = table.getjsonarray("menu"); //jsonarray data = new jsonarray(getjsonurl(url)); final arraylist<hashmap<string, string>> myarrlist = new arraylist<hashmap<string, string>>(); hashmap<string, string> map; for(int = 0; < data.length(); i++){ jsonobject c = data.getjsonobject(i); map = new hashmap<string, string>(); map.put("menuid", c.getstring("menuid")); map.put("name", c.getstring("name")); map.put("description2", c.getstring("description2")); map.put("price", c.getstring("price")); myarrlist.add(map); } simpleadapter sadap; sadap = new simpleadapter(_appetizer.this, myarrlist, r.layout.menulist_arrangement, new string[] {"name", "description2", "price", "menuid"}, new int[] {r.id.lblname, r.id.lbldesc2, r.id.lblprice, r.id.lblmenuid}); mlistview.setadapter(sadap); final alertdialog.builder viewdetail = new alertdialog.builder(this); // onclick item mlistview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> myadapter, view myview, int position, long mylng) { string _name = myarrlist.get(position).get("name") .tostring(); string _desc2 = myarrlist.get(position).get("description2") .tostring(); string _price = myarrlist.get(position).get("price") .tostring(); string _menuid = myarrlist.get(position).get("menuid") .tostring(); //string smemberid = ((textview) myview.findviewbyid(r.id.colmemberid)).gettext().tostring(); // string sname = ((textview) myview.findviewbyid(r.id.colname)).gettext().tostring(); // string stel = ((textview) myview.findviewbyid(r.id.coltel)).gettext().tostring(); viewdetail.seticon(android.r.drawable.btn_star_big_on); viewdetail.settitle(_name); viewdetail.setmessage("name : " + _name + "\n" + "description : " + _desc2 + "\n" + "price : " + _price + "\n" + "id: " + _menuid); viewdetail.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub dialog.dismiss(); } }); viewdetail.show(); } }); } catch (jsonexception e) { // todo auto-generated catch block log.e("log", "failed" +e); e.printstacktrace(); } double check jsonparser class. or java code. cant tell null pointer.
after reading source code , exception log, think root cause of npe trying perform network operation on ui thread, forbidden google on android 3+. can use asynctask access network.
Comments
Post a Comment