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);

    }
8
  • The index is 0-based, e.g. Size 5 has only valid indexes 0..4 Commented Mar 14, 2017 at 7: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 Commented Mar 14, 2017 at 7:43
  • No, because you don't show the initialization code for view[][] Commented Mar 14, 2017 at 7:46
  • view is just the instantiation of Cell which is just a 2D array Commented Mar 14, 2017 at 7:56
  • Also shouldn't this code catch an OutOfBoundsException?: if (it.hasNext() && stationList.get(stationIncrementer) != null Commented Mar 14, 2017 at 7:58

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks forgot to take that out, doesn't really solve my problem though
While your're correct that the code is unreachable, it has nothing to do with the problem and the question. You should post such information as comment, not as answer, which is meant for actual answers to the question.
Got it. Thankyou

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.