user5669913
user5669913

Reputation:

ArrayIndexOutOfBound exception

I am trying to sync google tasks with my application. For this I have created a class in which I have created all the methods which needs to get task lists and create the list etc.

Now I want to test If these methods are working or not. For this I have created a class which extends an AsyncTask.

I have to pass some parameters in this task as in methods I have parameters. Like in getTaskList(Strig listId). To get tasks from any list I will need to provide an id of a list.

Now when I tried to pass parameters in AsyncTask. While calling it in

result = gTaskSyncer.getTaskList(params[0].toString());

its throwing an ArrayIndexOutOfBoundException here.

TestAsyncTask

 public class TestAsyncTask extends AsyncTask<Object,Void,List<Task>>{

    private com.google.api.services.tasks.Tasks mService = null;
    private Exception mLastError = null;
    private MainActivity activity;
    private com.google.api.services.tasks.Tasks client = null;

    public TestAsyncTask(GoogleAccountCredential credential, MainActivity activity) {
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        mService = new com.google.api.services.tasks.Tasks.Builder(
                transport, jsonFactory, credential)
                .setApplicationName("Google Tasks API Android Quickstart")
                .build();
        this.activity = activity;
    }

    protected List<Task> doInBackground(Object... params) {



        GTaskSyncer gTaskSyncer = new GTaskSyncer(activity);

        List<Task> result = new ArrayList<>();

        try {

            result = gTaskSyncer.getTaskList(params[0].toString());

        } catch (IOException e) {

            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
                }
            });

        }
        return result;
    }


    @Override
    protected void onPostExecute(List<Task> output) {
        activity.mProgress.hide();
        if (output == null || output.size() == 0) {
            activity.mOutputText.setText("No results returned.");
        } else {
            activity.mOutputText.setText(TextUtils.join("\n", output));
        }
    }
}

GTaskSyncer

 public class GTaskSyncer
{

    final MainActivity activity;
    final com.google.api.services.tasks.Tasks mService;
    private Exception mLastError = null;



    GTaskSyncer(MainActivity activity) {
        this.activity = activity;
        mService = activity.mService;
    }

    public List<TaskList> getAllTaskList() throws IOException
    {
        List<TaskList> result = new ArrayList<TaskList>();

        TaskLists taskLists = mService.tasklists().list().execute();

        for (TaskList taskList : taskLists.getItems()) {

                result.add(taskList);
        }

        return result;

    }


    public TaskList createList() throws IOException
    {

        TaskList taskList = new TaskList();

        taskList =  activity.mService.tasklists().insert(taskList).execute();

        return taskList;
    }

    public Task createTask(String listId) throws IOException
    {

        Task task = new Task();

        task =   activity.mService.tasks().insert(listId, task).execute();

        return  task;
    }

    public Task getTask(String listId,String taskId) throws IOException
    {

        Task task =   activity.mService.tasks().get(listId, taskId).execute();

        return task;
    }

    public List<Task> getTaskList(String listId) throws IOException {


        List<Task> result = new ArrayList<Task>();

        List<Task> tasks = mService.tasks().list(listId).execute().getItems();

        if (tasks != null) {

            for (Task task : tasks) {

                result.add(task);
            }
        } else {

            Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
        }

        return result;

    }

}

Also how can I test this? How to provide a listId to method when we call it?

Upvotes: 0

Views: 271

Answers (3)

Doug Stevenson
Doug Stevenson

Reputation: 317487

When you call execute() on an AsyncTask without any arguments, that missing argument to execute() becomes an empty (varargs) array in the AsyncTask's doInBackground() method.

If you want to be able to use parameters in doInBackground(), then you must pass some parameters via execute(). If you want to make sure you don't index past the list of items passed in execute, iterate the vararg arg array instead of indexing directly into it without checking its length.

Upvotes: 2

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

An array out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array limit, hence the words "Out of bounds".

Problem

 result = gTaskSyncer.getTaskList(params[0].toString()); // In here Problem 

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

  • I assume in your doInBackground section result is illegal index .

Upvotes: 0

Zahidul Islam
Zahidul Islam

Reputation: 3190

First check that, you are passing a value when you are calling the Asynctask class . ArrayIndexOutOfBoundException occurs because you are not passing but want to access that.

So, before access that parameter first check that if the size is greater than 1.

Upvotes: 0

Related Questions