0

This code is about a water tanker wandering an environment looking for water stations that have tasks.

Trying to increment through an ArrayList of points to visit but every time I run the code I get an "IndexOutOfBoundsException" but at different indexes and the size is always the same as the index so I am very confused. The index/size that break the program appear to change randomly.

Example error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5

Relevant code(Assume it runs infinitely):

int stationIncrementer = 0;
ArrayList<Point> stationList = new ArrayList<Point>();
ArrayList<Point> wellList = new ArrayList<Point>();
Iterator it = stationList.iterator();

public Action senseAndAct(Cell[][] view, long timestep) {

    // Loop to scan every cell in view for stations
    int i = 0, j = 0;
    for (i = 0; i < 25; i++) {
        for (j = 0; j < 25; j++) {
            Cell c = view[i][j];
            if (view[i][j] instanceof Station) {
                Task t = ((Station) c).getTask();
                Point p = view[i][j].getPoint();
                if (stationList.contains(p)) {
                    break;
                } else if (t != null) {
                    stationList.add(p);
                }
            }
            if (j == 25) {
                j = 0;
            }
    }
}

if (it.hasNext() && stationList.get(stationIncrementer) != null && getWaterLevel() > 0
            && !(getCurrentCell(view) instanceof Station)) {
        Point p = stationList.get(stationIncrementer);
        stationIncrementer++;
        return new MoveTowardsAction(p);

    }
Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
killiantos
  • 1
  • 1
  • 2
  • The index is 0-based, e.g. Size 5 has only valid indexes 0..4 – Lucero Mar 14 '17 at 07:41
  • Can you see any reason why the index is randomly going out of bounds then? As I said the error has occured for several different values – killiantos Mar 14 '17 at 07:43
  • No, because you don't show the initialization code for view[][] – Lucero Mar 14 '17 at 07:46
  • view is just the instantiation of Cell which is just a 2D array – killiantos Mar 14 '17 at 07:56
  • Also shouldn't this code catch an OutOfBoundsException?: if (it.hasNext() && stationList.get(stationIncrementer) != null – killiantos Mar 14 '17 at 07:58
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Manuel Allenspach Mar 14 '17 at 08:13
  • If the "2D-Array" (there is actually no such thing in Java, just [jagged arrays](https://en.wikipedia.org/wiki/Jagged_array)) is not initialized correctly you'd get OutOfBounds errors, so it does matter... – Lucero Mar 14 '17 at 09:02
  • Why are you iterating using hardcoded ranges in the first place? You should use either `i < view.length` and `j < view[i].length` or use *enhanced `for` loops* – UnholySheep Mar 14 '17 at 09:36

1 Answers1

0

Your condition if(j==25) will never execute as for loop condition defined is j<25, correct it.

ankitkhandelwal185
  • 1,023
  • 1
  • 15
  • 24