I get this error "Sometimes" in my App, I use a Asyntask:
03-19 08:10:05.768: E/AndroidRuntime(280): FATAL EXCEPTION: main
03-19 08:10:05.768: E/AndroidRuntime(280): java.lang.NumberFormatException: unable to parse 'null' as integer
03-19 08:10:05.768: E/AndroidRuntime(280): at java.lang.Integer.parseInt(Integer.java:406)
03-19 08:10:05.768: E/AndroidRuntime(280): at java.lang.Integer.parseInt(Integer.java:382)
03-19 08:10:05.768: E/AndroidRuntime(280): at java.lang.Integer.valueOf(Integer.java:682)
03-19 08:10:05.768: E/AndroidRuntime(280): at Dic.proj.pkg.notifService$1$1$1$1.onPostExecute(notifService.java:76)
03-19 08:10:05.768: E/AndroidRuntime(280): at Dic.proj.pkg.notifService$1$1$1$1.onPostExecute(notifService.java:1)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
03-19 08:10:05.768: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-19 08:10:05.768: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:10:05.768: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
03-19 08:10:05.768: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-19 08:10:05.768: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-19 08:10:05.768: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
i call my Asyntask inside a timer. this is my my Asyntask:
// fetch new notifications
class fetch_last_asyn extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... arg0) {
RestClient client = new RestClient(
"http://myaddress.org/api/get_last.php");
client.AddParam("last", String.valueOf(last_notif_no));
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
String response = client.getResponse();
last_notifs = response.toString();
return true;
}
}
Inside my app I send my Asyntask result to parse to a code (parse_get_update), this is it:
public static String[][] parse_get_update(String jsonResponse) {
String[][] notifs = new String[3000][10];
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject json = jsonArray.getJSONObject(index);
// get name & id here
String id = json.getString("id");
String notifno = json.getString("notifno");
String title = json.getString("title");
String description = json.getString("description");
notifs[index][0] = id;
notifs[index][1] = notifno;
notifs[index][2] = title;
notifs[index][3] = description;
// notif_count++;
}
cnt2 = jsonArray.length();
} catch (JSONException e) {
e.printStackTrace();
}
return notifs;
}
Why do I get this error? What is NULL? I cant understand this.
I use ResClient for parse JSON. This error occures sometimes only, about 10% of times...
Thanks for your answers
In your postExecute()
method, somewhere, you're parsing a String
to Integer
using this - Integer.parseInt(str);
Most of the times that str
is a valid number, hence, its working. But those 10% of times, that str
is probably missing or coming as an emtpy
String from the JSON
and hence, a null
is passed to the Integer.parseInt(null)
, which is causing the java.lang.NumberFormatException.
Identify that piece of code, and put a null
check for that, before parsing it to Integer
.