Nested for loop error with !null checking an element that doesn't exist

Posted by Programatt on Stack Overflow See other posts from Stack Overflow or by Programatt
Published on 2012-11-19T16:48:45Z Indexed on 2012/11/19 17:00 UTC
Read the original article Hit count: 219

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about java

Related posts about for-loop