Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this background task fail with a fatal exception?

The following code in my MainActivity is failing 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 messages, with a smidgin of pre-exception context 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 with this - it fails at the git-go of doInBackground(). Is my URL "bad" due to the whacks (in C#, I would use a verbatim string), or...?

UPDATE

I removed the problematic line, yet it still fails, throwing an IOException.

I had actually switched gears to completely different code as can be seen here [Why am I getting, "incompatible types: Object cannot be converted to String" with this? (which was throwing NetworkOnMainThreadException), but when that failed I decided to give this code another chance, but the "catch (IOException e) " block is entered.

Does anybody know why?

like image 486
B. Clay Shannon-B. Crow Raven Avatar asked Dec 13 '25 13:12

B. Clay Shannon-B. Crow Raven


2 Answers

You forgot to pass any params to your AsyncTask here:

 new NetworkTask().execute();

That is why your array is empty here:

 String link = params[0];

==> pass a parameter while executing the AsyncTask:

 new NetworkTask().execute(someLink); //execute AsyncTask with one param

Update: in fact seems like you don't do anything with String link in the doInBackground method. So maybe you can just get rid of that line?

like image 66
donfuxx Avatar answered Dec 16 '25 09:12

donfuxx


This exception:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

is telling you that you are accessing an empty array at index 0, in this line:

protected HttpResponse doInBackground(String... params) {
     String link = params[0];

nota that after the assignment you dont use the link variable anywhere (nor the params).

like image 42
guido Avatar answered Dec 16 '25 10:12

guido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!