Java – why did this background task fail due to a fatal exception?
The following code in my mainactivity failed with a fatal exception in the overridden doinbackground() method:
public class MainActivity extends ActionBarActivity {
. . .
public void onFetchBtnClicked(View v){
if(v.getId() == R.id.FetchBtn){
Toast.makeText(getApplicationContext(),"You mashed the button,dude.",Toast.LENGTH_SHORT).show();
new NetworkTask().execute();
}
}
public static class NetworkTask extends AsyncTask<String,Void,HttpResponse> {
@Override
protected HttpResponse doInBackground(String... params) {
String link = params[0];
HttpGet request = new HttpGet("http://localhost:28642/api/deliveries/Count");
androidhttpclient client = androidhttpclient.newInstance("Android");
try {
return client.execute(request);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
client.close();
}
}
@Override
protected void onPostExecute(HttpResponse result) {
FileOutputStream f = null;
//Do something with result
if (result != null)
/* result.getEntity().writeTo(new FileOutputStream(f)); */
try {
result.getEntity().writeTo(f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The specific exception message of smidgin with exception context is as follows:
03-31 18:04:26.350 1401-1401/hhs.app I/Choreographer﹕ Skipped 33 frames! The application may be doing too much work on its main thread.
03-31 18:05:00.030 1401-1420/hhs.app W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0xb2a7aba8)
03-31 18:05:00.250 1401-1420/hhs.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: hhs.app,PID: 1401
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.Arrayindexoutofboundsexception: length=0; index=0
at hhs.app.MainActivity$NetworkTask.doInBackground(MainActivity.java:68)
at hhs.app.MainActivity$NetworkTask.doInBackground(MainActivity.java:64)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
What's the problem - it failed in Git - go of doinbackground() Is my web address "bad" due to attack (in c#, I will use verbatim string), or?
UPDATE
I deleted the problematic row, but it still failed, throwing IOException
I actually switched to a completely different code. Here we can see why I got the "incompatible types: objects cannot be converted to strings" (it is throwing networkonmainthreadexception), but when it failed, I decided to give the code another chance, but entered the "catch (IOException E)" block
Does anyone know why?
Solution
This exception:
Caused by: java.lang.Arrayindexoutofboundsexception: length=0; index=0
Tells you that you are accessing an empty array at index 0, in this line:
protected HttpResponse doInBackground(String... params) {
String link = params[0];
Note that you should not use linked variables (nor params) anywhere after the task
