Java Array out of bounds error isn't out of bounds? <p>This code is the relevant portion of my program, and it produces an array out of bounds error, and I can't figure out why. My error is 'java.lang.ArrayIndexOutOfBoundsException: 6', with 6 being a random value, at the if statement in randomShot();</p> <pre><code>public class Ai { private int WIDTH; private int HEIGHT; public Ai(){ WIDTH=10; HEIGHT=10; } int[][] board=new int[WIDTH][HEIGHT]; Random rand = new Random(); public void randomShot(){ x=rand.nextInt(WIDTH/2); y=rand.nextInt(HEIGHT); x=x*2; if(y%2==0) { y+=1; } if(board[x][y]!=0) //java.lang.ArrayIndexOutOfBoundsException: 6 { randomShot(); } } </code></pre> <p>I have noticed that if I use the code</p> <pre><code>int[][] board=new int[10][10]; </code></pre> <p>it works perfectly fine. I can't see why this is happening, it's doing exactly the same thing?</p>