2

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.

Michaël
  • 3,599
  • 7
  • 37
  • 61
Fcoder
  • 8,466
  • 17
  • 54
  • 90

3 Answers3

2

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 ?

LaGrandMere
  • 10,007
  • 1
  • 30
  • 40
1

Below code will work:

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

        try {
                JSONObject json = new JSONObject(jsonResponse);
                if(json!=null)
                   update = json.getString("update");
                else
                   update = "no";
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(update == null){
            update="no";
        }
        return update;
    }
Ankit HTech
  • 1,783
  • 5
  • 27
  • 42
1

Check if the String "update" is null with:

if (!json.isNull("update")) {
    update = json.getString("update");
}

If you dont check this, it can raise a nullpointer

PaNaVTEC
  • 2,459
  • 1
  • 21
  • 36