0

I've got a very annoying problem with some code throwing an IndexOutOfBoundsException and I really cannot understand why. The logcat points to the "addTimetableItem" of the following code which ill explain more on:

if(sortedFridayTimes.size()>0){
    insertDay("Friday");
    for(int i=1; i<sortedFridayTimes.size()+1;i++){
        addTimetableItem(sortedFridayTimes.get(i));
    }
}

"sortedFridayTimes" is an ArrayList containing my own "Timetable Entry" objects which I have sorted into order already. First the size is checked to see if there are any objects and if there is then "insertDay" runs which creates a new textview for a title and adds it to the layout (This works fine..). Inside the for loop the idea is to then add all the objects from the arraylist into the layout. Now I know that the "addTimetableItem" code works as ive tested it already, but my problem is that i cant seem to get the last object out of the arraylist. If I declare the for loop to only run for

"i<sortedFridayTimes.size()" 

then the program runs fine but I don't get the last entry in the arraylist which I know exists because I've debugged and watched my variables. On adding the "+1" as shown above I now get the IndexOutOfBoundsException and I really don't know why. As I've said, I've debugged and I know that an entry exists in the arraylist where I'm trying to point to, but it just crashes. I can provide more code if needs be, but does anyone have any ideas please?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Josh Suckling
  • 217
  • 7
  • 14

3 Answers3

2

You should accept @Tim's or @Graham's answer, this is just an addendum. They're correct about your size()+1 going past the end of the array.

If you're having difficulty using indexes to properly get everything out of the list, you can also try using a for-each loop (depending on the version of the Android SDK you're using). I'm assuming sortedFridayTimes is a list of class TimetableItem since you don't specify.

So this:

if(sortedFridayTimes.size()>0){
    insertDay("Friday");
    for(int i=1; i<sortedFridayTimes.size()+1;i++){
        addTimetableItem(sortedFridayTimes.get(i));
    }
}

Becomes this:

if(!sortedFridayTimes.isEmtpy()){
    insertDay("Friday");
    for(TimetimeItem item : sortedFridayTimes){
        addTimetableItem(item);
    }
}

A little cleaner if you don't actually need to use i anywhere.

Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56
  • Android best practice also recommends using the enhanced for loop for more efficient programs. This is a great answer. – jjNford Apr 21 '12 at 20:10
1
i<sortedFridayTimes.size()+1

You are looping past the last element in the array. Why the +1?

If there are N elements in the array, then the elements are from indexes 0 through to N-1.

So it should be:

for(int i=0; i<sortedFridayTimes.size(); i++) {
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Well I thought that but if say the size was 2, i would start at 1 and would be less than 2 and therefore the code would execute, but then i would become 2 which isnt less than 2 and therefore the code wouldnt run and i would miss the 2nd value in the list..right? – Josh Suckling Apr 21 '12 at 19:36
  • Cheers for the help, sorted now. – Josh Suckling Apr 21 '12 at 19:41
1

The last loop in your for loop runs:

sortedFridayTimes.get(sortedFridayTimes.size())

This will always be out of bounds, because the elements are zero indexed.

For example, if the array size is "5", then you cannot access index "5", because the 5 elements in the array are 0,1,2,3,4.

Tim
  • 35,413
  • 11
  • 95
  • 121