I fetch information from a json source from web inside an android service, inside a timer loop. some times, about 5% of times i get this error:
03-20 09:27:06.901: E/AndroidRuntime(401): FATAL EXCEPTION: main
03-20 09:27:06.901: E/AndroidRuntime(401): java.lang.NullPointerException
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONObject.<init>(JSONObject.java:154)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONObject.<init>(JSONObject.java:171)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService.parse_if_update(notifService.java:182)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:151)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:1)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.Looper.loop(Looper.java:123)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-20 09:27:06.901: E/AndroidRuntime(401): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 09:27:06.901: E/AndroidRuntime(401): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 09:27:06.901: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-20 09:27:06.901: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-20 09:27:06.901: E/AndroidRuntime(401): at dalvik.system.NativeStart.main(Native Method)
i user RestClient libraryfot parsing json. and this is my parse_if_update Asyntask:
public static String parse_if_update(String jsonResponse) {
String update = "";
try {
JSONObject json = new JSONObject(jsonResponse);
update = json.getString("update");
} catch (JSONException e) {
e.printStackTrace();
}
return update;
}
whats wrong with this? i think it related to my asyntask because my asyntask runs several times sometimes...
The NullPointerException
is thrown by the JSONTokener
, which makes me think that your jsonResponse
object is null
. Add a check for this and you should be fine:
if (jsonResponse != null) {
JSONObject json = new JSONObject(jsonResponse);
update = json.getString("update");
}
else
update = "response was null!";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.