Search code examples
javaandroidjsonrest-client

error in parsing json inside an AsynTask in Android


I have an AsynTask and trying to retrieve a json and parse it, but I get this error:

03-21 11:38:07.033: E/AndroidRuntime(8439): FATAL EXCEPTION: main
03-21 11:38:07.033: E/AndroidRuntime(8439): java.lang.NullPointerException
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONObject.<init>(JSONObject.java:154)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONObject.<init>(JSONObject.java:171)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService.parse_if_update(notifService.java:191)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:153)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:1)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.Looper.loop(Looper.java:137)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.app.ActivityThread.main(ActivityThread.java:4921)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at java.lang.reflect.Method.invoke(Method.java:511)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at dalvik.system.NativeStart.main(Native Method)

and this is my parse_if_update function:

public static String parse_if_update(String jsonResponse) {
    String update="no";

    try {
        JSONObject json = new JSONObject(jsonResponse);
        update = json.getString("update");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(update == null){
        update="no";
    }
    return update;
}

What's wrong? I spend two days to solve this problem. Sometimes I encounter null values but I don't know how to treat with them.


Solution

  • Add simple different from null tests.

    public static String parse_if_update(String jsonResponse) {
    String update="no";
    
        if (jsonResponse != null) {    
            try {
                    JSONObject json = new JSONObject(jsonResponse);
                    if (json != null) { 
                        update = json.getString("update");
                    }
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            if(update == null){
                update="no";
            }
        }
        return update;
    }
    

    Do you still have the problem then ?