Nested for loop error with !null checking an element that doesn't exist
- by Programatt
I am currently using nested for loops in a 2D array of size 4,2.
When I run my program, I get index out of bounds Exception on the following line
else if (state[i][j+1] != null
&& state[i][j].getFlash() <= state[i][j].getCycleLength()
&& state[i][j+1].getCycleLength() == state[i][j].getCycleLength()){
}
It says the index out of bounds is 2. I would understand the error if I wasn't checking to see if [i][j+1] wasn't null, but I don't understand the exception with the check? I tried moving around the !null check but the program still fails on this line.
Any help would be greatly appreciated.
Stack trace:
Exception in thread "Timer-0" java.lang.ArrayIndexOutOfBoundsException: 2
at NatComp.data$1.run(data.java:67)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)