android - Null pointer exception: unable to parse the Json data. (null object reference) -


iam trying data url. have read lot of post regarding same question nothing works. please me out slove issue... ill post code below

jsonparser.class

import android.util.log;  public class jsonparser {      final string tag = "jsonparser.java";      static inputstream = null;     static jsonobject jobj = null;     static string json = "";      public jsonobject getjsonfromurl(string url) {          // make http request         try {              defaulthttpclient httpclient = new defaulthttpclient();             //httppost httppost = new httppost(url);             httpget httpget = new httpget(url);             httpresponse httpresponse = httpclient.execute(httpget);             httpentity httpentity = httpresponse.getentity();              string json = entityutils.tostring(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(tag, "error converting result " + e.tostring());         }          // try parse string json object         try {             jobj = new jsonobject(json);         } catch (jsonexception e) {             log.e(tag, "error parsing data " + e.tostring());         }          // return json string         return jobj;     } } 

here mainactivity

public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // using asynctask during parsing          new asynctaskparsejson().execute();     }      // can make class java file separated main activity.     public class asynctaskparsejson extends asynctask<string, string, string> {          final string tag = "asynctaskparsejson.java";          // set json string url here         string yourjsonstringurl = "http://localhost:8083/android/search";          // contacts jsonarray         jsonarray datajsonarr = null;          @override         protected void onpreexecute() {}          @suppresslint("newapi")         @override         protected string doinbackground(string... arg0) {              try {                  // instantiate our json parser                 jsonparser jparser = new jsonparser();                  // json string url                 jsonobject json = jparser.getjsonfromurl(yourjsonstringurl);                  // array of users                 datajsonarr = new jsonarray(json.getjsonarray("searchlist"));                  // loop through users                 (int = 0; < datajsonarr.length(); i++) {                      //jsonobject c = datajsonarr.getjsonobject(i);                      //string firstname = c.getstring("firstname");                     string firstname= datajsonarr.getstring(i);                     // show values in our logcat                     log.e(tag, "firstname: " + firstname                              );                  }              } catch (jsonexception e) {                 e.printstacktrace();             }              return null;         }          @override         protected void onpostexecute(string strfromdoinbg) {}     } } 

androimanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.androidjsonparsing"     android:versioncode="1"     android:versionname="1.0" >      <uses-permission android:name="android.permission.internet"></uses-permission>      <uses-sdk         android:minsdkversion="8"         android:targetsdkversion="17" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.example.androidjsonparsing.mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

this json file url:

  {"searchlist":["micromax ninja a89 smartphone","capdase fliptop case samsung galaxy s ii i9100","ifb microwave convection 20sc2, 20 litres","samsung microwave convection ce1041dfb, black, 28 litres"]} 

this log file:

04-23 13:34:49.621: e/jsonparser.java(955): error converting result java.lang.nullpointerexception: lock == null 04-23 13:34:49.622: e/jsonparser.java(955): error parsing data org.json.jsonexception: end of input @ character 0 of  04-23 13:34:49.622: e/jsonparser.java(955): --------- beginning of crash 04-23 13:34:49.662: e/androidruntime(955): fatal exception: asynctask #1 04-23 13:34:49.662: e/androidruntime(955): process: com.example.androidjsonparsing, pid: 955 04-23 13:34:49.662: e/androidruntime(955): java.lang.runtimeexception: error occured while executing doinbackground() 04-23 13:34:49.662: e/androidruntime(955):  @ android.os.asynctask$3.done(asynctask.java:304) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.futuretask.run(futuretask.java:242) 04-23 13:34:49.662: e/androidruntime(955):  @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 04-23 13:34:49.662: e/androidruntime(955):  @ java.lang.thread.run(thread.java:818) 04-23 13:34:49.662: e/androidruntime(955): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'org.json.jsonarray org.json.jsonobject.getjsonarray(java.lang.string)' on null object reference 04-23 13:34:49.662: e/androidruntime(955):  @ com.example.androidjsonparsing.mainactivity$asynctaskparsejson.doinbackground(mainactivity.java:50) 04-23 13:34:49.662: e/androidruntime(955):  @ com.example.androidjsonparsing.mainactivity$asynctaskparsejson.doinbackground(mainactivity.java:1) 04-23 13:34:49.662: e/androidruntime(955):  @ android.os.asynctask$2.call(asynctask.java:292) 04-23 13:34:49.662: e/androidruntime(955):  @ java.util.concurrent.futuretask.run(futuretask.java:237) 04-23 13:34:49.662: e/androidruntime(955):  ... 4 more 

in mainactivity

change line:

datajsonarr = new jsonarray(json.getjsonarray("searchlist")); 

to:

datajsonarr = new json.getjsonarray("searchlist"); 

otherwise coding work.

check url provided correct or not.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -