0

I know that this question has been asked a million times. And I feel like the solution will be fairly obvious to someone who hasn't been staring at it for a couple of hours. But I can't make head or tails of my out of bound exception. Here is the error:

exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 207493, Size: 207493
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at affysureselect.AffySureSelect.main(AffySureSelect.java:92)
Java Result: 1

I was thinking perhaps this might be happening due to the size of the arraylist, but if that were the case I would have expected the error to be when adding, rather than the getting. Here is the code where it is dying:

    String chrompos;
    ArrayList<String> chromnum = new ArrayList<String>();
    while ((input2 = sbuff.readLine()) != null) {
        prse = input2.split("\t");
        chromnum.add(prse[0]);
        ...
        chrompos = prse[7];
     }
    int cnt = 0;
    int cnt2 = 0;
    if (chromnum.get(cnt).equals(chrompos)) { // line causing my untimely death
      end = Integer.parseInt(chromposend.get(cnt2));
      start = Integer.parseInt(chromposstart.get(cnt2));
       ...

I even tried adding:

if (cnt <= chromnum.size()) { //this line
  if (chromnum.get(cnt).equals(chrompos)) { /before the dying line

But it dies anyway, on the get, not the add. What am I missing?

2
  • System.out.println the list, and you'll see you have less elements than you think Commented Apr 5, 2013 at 3:48
  • 1
    The problem is that you're confusing 0-based indexing with 1-based indexing. If your list has 10 elements, the indexes that the elements exist at are 0-9 (when you just use ArrayList.add( Object). Your check to make sure an index exists in the ArrayList would be if (cnt < chromnum.size()). Commented Apr 5, 2013 at 3:50

7 Answers 7

1

If you are incrementing cnt, make sure that it's always less that chromnum.size().

It should be-

if (cnt < chromnum.size())
Sign up to request clarification or add additional context in comments.

1 Comment

Oh. Duh. I knew it would be a really silly thing. Thanks so much. I will accept in 11 minutes.
0

Index i does not exist. You must always iterate to list.size() - 1.

Comments

0

You can't access the index i if the size of the ArrayList is also i. The maximum accessible index is i-1. Hence, the IndexOutOfBoundsException which says you're trying to access the 207493th index, even when the size of your List is also 207493.

Have a check to restrict cnt below the size of your list.

Comments

0

Since, as per the code it looks like, that in if loop you are trying a

 chromnum.get(cnt) and cnt is initialized to 0.

So the most probable issue is that the code never entered the while-loop, juts put a SOP after the while loop and check for the arraylist size.

Comments

0

Remove the equal sign,

if (cnt < chromnum.size()) { //this line
      if (chromnum.get(cnt).equals(chrompos)) {

Hope to help you:)

Comments

0

Remember that lists start with 0, so if you have a list of N items the last item will be N - 1 (because 0 is the 1st element, 1 is the 2nd and so on)

So you should write

if (cnt < chromnum.size()) {

instead of

if (cnt <= chromnum.size()) {

Comments

0

1-based like Pascal: United States, English-speaking Canada.
0-based like C/Java: Quebec, (most of?) Europe.

That's the answer. The question is, How are floors indexed in an elevator.

Comments

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.