2
CopyOnWriteArrayList<Options> Options= new CopyOnWriteArrayList<Options>();
Options Options = Options.get(selectedFilterIndex);

I am getting the error in the above line. The Options is an ArrayList. I tried with ArrayList as well as CopyOnWriteArrayList, but get the below error. How handle such exceptions.

06-21 15:28:23.257: E/XXX(8985): Uncaught exception is: 
06-21 15:28:23.257: E/XXX(8985): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 15:28:23.257: E/XXX(8985):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-21 15:28:23.257: E/XXX(8985):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-21 15:28:23.257: E/XXX(8985):  at java.lang.Thread.run(Thread.java:856)
06-21 15:28:23.257: E/XXX(8985): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
06-21 15:28:23.257: E/XXX(8985):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
06-21 15:28:23.257: E/XXX(8985):  at java.util.ArrayList.get(ArrayList.java:304)
06-21 15:28:23.257: E/XXX(8985):  at com.usercontext.CommonActivity$6.doInBackground(AppointmentsActivity5.java:354)
06-21 15:28:23.257: E/XXX(8985):  at com.usercontext.CommonActivity$6.doInBackground(AppointmentsActivity5.java:1)
06-21 15:28:23.257: E/XXX(8985):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-21 15:28:23.257: E/XXX(8985):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
3
  • did you check for selectedFilterIndex <= options.size(). I think selectedFilterIndex is growing and becomes greater than options.size(). Commented Jun 21, 2013 at 10:23
  • @VaibhavRaj It is not, since options.size() is 0. The list is empty. Commented Jun 21, 2013 at 10:23
  • 1
    Then, its obvious that you will get this error. Since, your list is of size zero and you are trying to access an index grater than 0. Commented Jun 21, 2013 at 10:25

2 Answers 2

5

You shouldn't be handling this exception - you should be writing code that isn't going to cause it in the first place.

You've just created an empty list - there's no value you can pass to get which will return a value. The list has a size of 0, and the index is meant to be greater than or equal to 0, and strictly less than the size.

Additionally, your code would be much simpler to read if you followed the Java naming conventions of making variables camelCased - that will avoid having the same name for both a variable and a type. Also avoid using the same variable name twice...

CopyOnWriteArrayList<Options> allOptions = new CopyOnWriteArrayList<Options>();
// This will still fail for any value of selectedFilterIndex, as the list
// is empty... but at least it's easier to understand.
Options selectedOptions = allOptions.get(selectedFilterIndex);
Sign up to request clarification or add additional context in comments.

Comments

1

You have just declared the List , there are no elements in it yet .

CopyOnWriteArrayList<Options> Options= new CopyOnWriteArrayList<Options>();

And you are trying to get() from the List with no elements . ArrayList internally will use an array buffer to store elements . According to the Javadoc get(int index):

Throws:

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Hence Options.get(selectedFilterIndex); will work only for (selectedFilterIndex>-1 && selectedFilterIndex < Options.size())

1 Comment

You might want to advise following Java naming conventions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.