intrepidkarthi
intrepidkarthi

Reputation: 3102

How to resolve Exception in AsyncTask

Here is my AsyncTask. Getting exception in the AsyncTask..

private class GetCategories extends AsyncTask<String, Void, String[]> {
    String temp[] = new String[] {};
    protected String[] doInBackground(String... urls) {
        JSONParser jParser = new JSONParser();
        JSONArray category_details;
        try {
            URL url = new URL(urls[0]);
            JSONObject json = jParser.getJSONFromUrl(url.toString());
            category_details = json.getJSONArray("cat");
            for (int i = 0; i < category_details.length(); i++) {
                JSONObject obj = category_details.getJSONObject(i);
                temp[i] = obj.getString("catName");
            }
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    protected void onPostExecute(String[] temp) {

    }

}

I am excuting the task and fetching the value like this.

  try {
        String[] test = new GetCategories().execute(categories_url).get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But I am getting error like this. I am getting exception inside the doInBackground method.

10-11 15:26:47.207: W/System.err(5158): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
10-11 15:26:47.207: W/System.err(5158):     at com.example.fragments.CatFragment$GetCategories.doInBackground(CategoriesFragment.java:207)
10-11 15:26:47.207: W/System.err(5158):     at com.example.fragments.CatFragment$GetCategories.doInBackground(CategoriesFragment.java:1)
10-11 15:26:47.207: W/System.err(5158):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-11 15:26:47.207: W/System.err(5158):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-11 15:26:47.207: W/System.err(5158):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-11 15:26:47.207: W/System.err(5158):     at  android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-11 15:26:47.207: W/System.err(5158):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-11 15:26:47.207: W/System.err(5158):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-11 15:26:47.207: W/System.err(5158):     at java.lang.Thread.run(Thread.java:856)

The line 207 goes to the catch block. Can anyone point out what I am doing wrongly here?

Upvotes: 0

Views: 1418

Answers (1)

Jamshid
Jamshid

Reputation: 340

i think, this exception caused by incorrect string array declaration you can use this this tutorial 1) Declaring a Java String array with an initial size If you know up front how large your array needs to be, you can (a) declare a String array and (b) give it an initial size like this:

public class JavaStringArrayTests
{
  private String[] toppings = new String[20];
  // more to the class here ...
}

In this example, we declare a String array named toppings, and then give it an initial size of 20 elements.

Later on, in a Java method in your class, you can populate the elements in the String array like this:

void populateStringArray()
{
  toppings[0] = "Cheese";
  toppings[1] = "Pepperoni";
  toppings[2] = "Black Olives";
  // ...
}

source: http://alvinalexander.com/java/java-string-array-reference-java-5-for-loop-syntax

Upvotes: 3

Related Questions