Confusing array out of bounds on game board
<p>This is a method in a code for a slightly more complex type of tic tac toe. The problem is, whenever the computer tries to make a move, it gives an array out of bounds -1 error, which is really confusing me because I made sure to specify in the code that it won't do that.</p>

<pre><code>    public void computerMove()
    {
       boolean moveMade=false;
       while(!moveMade)
       {                                                                                                                                                                   
          int i = (int)(Math.random()*5);
          int j = (int)(Math.random()*4);
          int p = (int)(Math.random()*5+1); 
          //whether the piece will move up, down, left or right
           if (grid[i][j].getText().equals("0")){
             if (p==1&amp;&amp;grid[i+1][j].getText().equals("")
             &amp;&amp;i&lt;3&amp;&amp;moveMade==false) 
             //here i specify when i&lt;3 to make sure it wont try to go off the board
              {
                grid[i][j].setText("");
                grid[i+1][j].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF();
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==2&amp;&amp;grid[i][j+1].getText().equals("")
             &amp;&amp;j&lt;3&amp;&amp;moveMade==false)
             {
                grid[i][j].setText("");
                grid[i][j+1].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF(); 
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==3&amp;&amp;grid[i][j-1].getText().equals(""
             )&amp;&amp;j&gt;0&amp;&amp;moveMade==false)
             {
                grid[i][j].setText("");
                grid[i][j-1].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF(); 
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else if (p==4&amp;&amp;grid[i-1][j].getText().equals("")&amp;&amp;
             i&gt;1&amp;&amp;moveMade==false)
             {
                grid[i][j].setText("");
                grid[i-1][j].setText("0");
                turn++; checkXHorizontal(); check0Horizontal(); 
                checkXVertical(); check0Vertical(); checkXDF();
                check0DF(); checkXDB(); check0DB();
                whoseTurn.setText("Player's Turn");
                moveMade=true;
             }
             else {
                computerMove(); 
             }
          }
       }
</code></pre>