-2

I am having trouble with an ArrayList, which sould fill a TableModel. In the 2nd for-loop the app crashes the 3rd time with an

java.lang.IndexOutOfBoundsException: Index: 14, Size: 14

at

if (al.get(i + 4)!=null)

and I dont know why, because i in this case is 10, so it checks for index 14, which actually is null. It should enter the else-loop, but instead it crashes. Thanks for your help, here the code:

String[] teile = tabelleninhalt.split("#");
ArrayList<String> al = new ArrayList<String>();
        for (int i = 1; i < teile.length; i++) {
            al.add(teile[i]);

        }
        for (int i = 0; i < al.size(); i = i + 5) {
            if (al.get(i + 4)!=null) {
                tabModel.addRow(new Object[] { al.get(i), al.get(i + 1),
                        al.get(i + 2), al.get(i + 3), al.get(i + 4) });
            } else {
                tabModel.addRow(new Object[] { al.get(i), al.get(i + 1),
                        al.get(i + 2), al.get(i + 3) });
            }
        }
AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Maik Fruhner
  • 300
  • 4
  • 14

6 Answers6

3

No, when the size is 14, there is no index 14. The valid indexes are 0 to 13 inclusive.

Your loop should look like this:

for (int i = 0; i < al.size() - 4; i += 5)

If your list is meant to have batches of 5 entries though, it sounds like you've got a problem if you've got a size of 14. Shouldn't the size always be a multiple of 5?

EDIT: If you want to treat a missing final value as effectively null, but still require the previous 4 fields, you may want:

for (int i = 0; i < al.size() - 3; i += 5) {
    if (i + 4 < al.size() && al.get(i + 4) != null) {
        ... // Use al.get(i + 4)
    } else {
        ... // Don't use it
    }
}

(But it's hard to tell based on a question which doesn't give any requirements.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I'm honored to only be 11 seconds behind @JonSkeet! :D –  Jan 14 '13 at 18:25
  • the string filled into the table is read out of a file and some parts can be left open, so it doesnt have to be a multiple of 5. Your solution doesnt work, because the last row wont be filled in. but thx anyway :) – Maik Fruhner Jan 14 '13 at 18:29
  • 2
    @MaikFruhner: Well it won't throw an exception - it's hard to give an answer which satisfies requirements when those requirements arne't specified in the question. Would you count any missing fields as effectively null? – Jon Skeet Jan 14 '13 at 18:30
  • It makes sense to me, that there are only 13 indexes, but the debugger said, that 14 and 15 == null, and i thought i could check, wheater this is true/false. The problem is, that there can be one last object to be filled in, but it can be left open. This has to be checked. – Maik Fruhner Jan 14 '13 at 18:34
  • @MaikFruhner: The debugger was probably showing the *underlying array* - which isn't the same as the logical list. (It's an implementation detail, basically.) – Jon Skeet Jan 14 '13 at 18:35
  • Thats it :) Thank you very much for your fast help! – Maik Fruhner Jan 14 '13 at 18:37
0

Index 14 is the 15th element of an ArrayList. Like most programming languages, arrays (and Lists) in Java are zero-indexed.

0

It's not null, that position doesn't even exist, that's why you get an IndexOutOfBoundsException exception.

pcalcao
  • 15,789
  • 1
  • 44
  • 64
0

If you try to access a location which is greater than the size of arraylist, you will be getting IOBE.You cant compare that location to null, since that location is not accessible.Better check for size rather than null.

Renjith
  • 3,274
  • 19
  • 39
0

It sounds like your array only has 14 items in it, stored in indexes 0-13. Trying to access index 14 will result in the exception.

Krease
  • 15,805
  • 8
  • 54
  • 86
0

The problem is that the index starts with 0 and the last element has the index 13. Accessing it with index 14 thus throws the IndexOutOfBoundException

Flo
  • 1,469
  • 1
  • 18
  • 27