Note that there are some explanatory texts on larger screens.

plurals
  1. POWays of handling isNull() method in JSON Parsing
    text
    copied!<p>I am facing a serious problem when parsing some json data from server. When it server returns some json data my program handle it well .But when the server returns null, my app gives me exceptions.</p> <p>I have search many questions and answers. Maximum answers are saying to use isNull() method to handle the null return type for JSON. But before writing isNull method we have to write </p> <pre><code> JSONObject json = new JSONObject("returned string"); </code></pre> <p>this sentence is worked fine when there is data for parsing, but it gives me Exception when the string is null.</p> <p>I think my question may be make some confusion to understand. So I am giving a bit of my code here.</p> <pre><code> URL Url = new URL(URL + "?userId=3"); HttpURLConnection urlConnection = (HttpURLConnection) Url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); // gets the server json data StringBuilder sb = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream())); String next = ""; while ((next = bufferedReader.readLine()) != null) { StringTokenizer tokens = new StringTokenizer(next); while(tokens.hasMoreTokens()) { Log.d("Tokens", tokens.nextToken()); } Log.d("next value", next); JSONObject json = new JSONObject(next); if (json.isNull("Location")) { Log.d("Json is null", "Ok"); serverLatitude = String.valueOf(lati); serverLongitude = String.valueOf(longi); } else { Log.d("Json not null", "Ok"); lat = json.getString("Latitude"); lon = json.getString("Longitude"); if (lat != null &amp;&amp; lon != null) { serverLatitude = lat; serverLongitude = lon; } else { } } </code></pre> <p>When there is data in server it works fine.But when the server returns null it give me error.So can anyone help me how i hanle this problem</p> <p><strong>Edit</strong></p> <p>This is the logcat when the server returns null.</p> <pre><code> 08-26 14:53:22.996: D/Tokens(31397): null 08-26 14:53:22.996: D/next value(31397): null 08-26 14:53:22.996: D/Here lat(31397): 12.90871405 08-26 14:53:22.996: W/System.err(31397): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject 08-26 14:53:22.996: W/System.err(31397): at org.json.JSON.typeMismatch(JSON.java:107) 08-26 14:53:22.996: W/System.err(31397): at org.json.JSONObject.&lt;init&gt; (JSONObject.java:158) 08-26 14:53:22.996: W/System.err(31397): at org.json.JSONObject.&lt;init&gt;(JSONObject.java:171) 08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.fetchLastInsertedRow(NewService.java:264) 08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.insertDataToServer(NewService.java:126) 08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:108) 08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:1) 08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask.finish(AsyncTask.java:417) 08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask.access$300(AsyncTask.java:127) 08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 08-26 14:53:23.006: W/System.err(31397): at android.os.Handler.dispatchMessage(Handler.java:99) 08-26 14:53:23.006: W/System.err(31397): at android.os.Looper.loop(Looper.java:130) 08-26 14:53:23.006: W/System.err(31397): at android.app.ActivityThread.main(ActivityThread.java:3806) 08-26 14:53:23.006: W/System.err(31397): at java.lang.reflect.Method.invokeNative(Native Method) 08-26 14:53:23.006: W/System.err(31397): at java.lang.reflect.Method.invoke(Method.java:507) 08-26 14:53:23.006: W/System.err(31397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-26 14:53:23.006: W/System.err(31397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-26 14:53:23.006: W/System.err(31397): at dalvik.system.NativeStart.main(Native Method) 08-26 14:53:23.006: D/AndroidRuntime(31397): Shutting down VM 08-26 14:53:23.006: W/dalvikvm(31397): threadid=1: thread exiting with uncaught exception (group=0x40018560) 08-26 14:53:23.006: E/AndroidRuntime(31397): FATAL EXCEPTION: main 08-26 14:53:23.006: E/AndroidRuntime(31397): java.lang.NumberFormatException: 08-26 14:53:23.006: E/AndroidRuntime(31397): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267) 08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.Double.parseDouble(Double.java:318) 08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.insertDataToServer(NewService.java:130) 08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:108) 08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:1) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask.finish(AsyncTask.java:417) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask.access$300(AsyncTask.java:127) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.Handler.dispatchMessage(Handler.java:99) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.Looper.loop(Looper.java:130) 08-26 14:53:23.006: E/AndroidRuntime(31397): at android.app.ActivityThread.main(ActivityThread.java:3806) 08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.reflect.Method.invokeNative(Native Method) 08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.reflect.Method.invoke(Method.java:507) 08-26 14:53:23.006: E/AndroidRuntime(31397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-26 14:53:23.006: E/AndroidRuntime(31397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-26 14:53:23.006: E/AndroidRuntime(31397): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p><strong>Thank You</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload