title: Array Index is "Out of Bounds". But I can't see what's wrong
body: <p>My IDE (TextPad) is giving me this error:
  java.lang.ArrayIndexOutOfBoundsException: 9</p>
  
  <p>I looked at the array on line 38  and can't see what the problem is. changing the array would totally screw up the program.</p>
  
  <pre><code>   import java.awt.*;
  import java.applet.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.util.Random;
  
  
  public class TicTacToeGame implements ActionListener {
      /*Define Buttons, Variables Etc*/
        int[][] winCombinations = new int[][] {
                      {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
                      {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //virticle wins
                      {1, 5, 9}, {3, 5, 7}
                  };//diagonal wins
  
       JFrame window = new JFrame("Tic-Tac-Toe Game");
       JButton btn[] = new JButton[9];
       int count = 0;
       JLabel lblTitle = new JLabel("Tic Tac Toe Game");
       JLabel lblBlank = new JLabel(" ");
       String letter = "";
       boolean win = false;
  
  
  
  
  
      public TicTacToeGame(){
      /*Creates Window*/
      window.setSize(400,300);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLayout(new GridLayout(3,3));
  
  
      /*Adds Buttons To The Window&amp; adds ActionListener to them*/
      for(int i = 1; i&lt;=9; i++){
              btn[i] = new JButton();
              window.add(btn[i]);
              btn[i].addActionListener(this);
          }
  
  
      /*Make The Window Visible*/
      window.setVisible(true);
      }
  
      public void actionPerformed(ActionEvent a) {
  
  
        /*Write the letter to the button and deactivate it*/
              for(int i = 1; i&lt;= 9; i++){
                  if(a.getSource() == btn[i]){
                      btn[i].setText("X");
                      btn[i].setEnabled(false);
                  }
              }
  
              count++;
              AI();
  
          }
  
  
            public void AI(){
                count++;
                if(btn[1].getText().equals("O") &amp;&amp; btn[2].getText().equals("O") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                } else if(btn[4].getText().equals("O") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[6].getText().equals("")){
                    btn[6].setText("O");
                    btn[6].setEnabled(false);
                } else if(btn[7].getText().equals("O") &amp;&amp; btn[8].getText().equals("O") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                }
  
                else if(btn[2].getText().equals("O") &amp;&amp; btn[3].getText().equals("O") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[5].getText().equals("O") &amp;&amp; btn[6].getText().equals("O") &amp;&amp; btn[4].getText().equals("")){
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                } else if(btn[8].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("O") &amp;&amp; btn[3].getText().equals("O") &amp;&amp; btn[2].getText().equals("")){
                    btn[2].setText("O");
                    btn[2].setEnabled(false);
                } else if(btn[4].getText().equals("O") &amp;&amp; btn[6].getText().equals("O") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                } else if(btn[7].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[8].getText().equals("")){
                    btn[8].setText("O");
                    btn[8].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("O") &amp;&amp; btn[4].getText().equals("O") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                } else if(btn[2].getText().equals("O") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[8].getText().equals("")){
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                } else if(btn[3].getText().equals("O") &amp;&amp; btn[6].getText().equals("O") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                }
  
                else if(btn[4].getText().equals("O") &amp;&amp; btn[7].getText().equals("O") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[5].getText().equals("O") &amp;&amp; btn[8].getText().equals("O") &amp;&amp; btn[2].getText().equals("")){
                    btn[2].setText("O");
                    btn[2].setEnabled(false);
                } else if(btn[6].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("O") &amp;&amp; btn[7].getText().equals("O") &amp;&amp; btn[4].getText().equals("")){
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                } else if(btn[2].getText().equals("O") &amp;&amp; btn[8].getText().equals("O") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                } else if(btn[3].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[6].getText().equals("")){
                    btn[6].setText("O");
                    btn[6].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("O") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                } else if(btn[5].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[1].getText().equals("O") &amp;&amp; btn[9].getText().equals("O") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                }
  
                else if(btn[3].getText().equals("O") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                } else if(btn[7].getText().equals("O") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                } else if(btn[7].getText().equals("O") &amp;&amp; btn[3].getText().equals("O") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[2].getText().equals("X") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                } else if(btn[4].getText().equals("X") &amp;&amp; btn[5].getText().equals("X") &amp;&amp; btn[6].getText().equals("")){
                    btn[6].setText("O");
                    btn[6].setEnabled(false);
                } else if(btn[7].getText().equals("X") &amp;&amp; btn[8].getText().equals("X") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                }
  
                else if(btn[2].getText().equals("X") &amp;&amp; btn[3].getText().equals("X") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[5].getText().equals("X") &amp;&amp; btn[6].getText().equals("X") &amp;&amp; btn[4].getText().equals("")){
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                } else if(btn[8].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[3].getText().equals("X") &amp;&amp; btn[2].getText().equals("")){
                    btn[2].setText("O");
                    btn[2].setEnabled(false);
                } else if(btn[4].getText().equals("X") &amp;&amp; btn[6].getText().equals("X") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                } else if(btn[7].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[8].getText().equals("")){
                    btn[8].setText("O");
                    btn[8].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[4].getText().equals("X") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                } else if(btn[2].getText().equals("X") &amp;&amp; btn[5].getText().equals("X") &amp;&amp; btn[8].getText().equals("")){
                    btn[8].setText("O");
                    btn[8].setEnabled(false);
                } else if(btn[3].getText().equals("X") &amp;&amp; btn[6].getText().equals("X") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                }
  
                else if(btn[4].getText().equals("X") &amp;&amp; btn[7].getText().equals("X") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[5].getText().equals("X") &amp;&amp; btn[8].getText().equals("X") &amp;&amp; btn[2].getText().equals("")){
                    btn[2].setText("O");
                    btn[2].setEnabled(false);
                } else if(btn[6].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[7].getText().equals("X") &amp;&amp; btn[4].getText().equals("")){
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                } else if(btn[2].getText().equals("X") &amp;&amp; btn[8].getText().equals("X") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                } else if(btn[3].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[6].getText().equals("")){
                    btn[6].setText("O");
                    btn[6].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[5].getText().equals("X") &amp;&amp; btn[9].getText().equals("")){
                    btn[9].setText("O");
                    btn[9].setEnabled(false);
                } else if(btn[5].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                } else if(btn[1].getText().equals("X") &amp;&amp; btn[9].getText().equals("X") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                }
  
                else if(btn[3].getText().equals("X") &amp;&amp; btn[5].getText().equals("X") &amp;&amp; btn[7].getText().equals("")){
                    btn[7].setText("O");
                    btn[7].setEnabled(false);
                } else if(btn[7].getText().equals("X") &amp;&amp; btn[5].getText().equals("X") &amp;&amp; btn[3].getText().equals("")){
                    btn[3].setText("O");
                    btn[3].setEnabled(false);
                } else if(btn[7].getText().equals("X") &amp;&amp; btn[3].getText().equals("X") &amp;&amp; btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("X") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[9].getText().equals("X")) {
                    btn[6].setText("O");
                    btn[6].setEnabled(false);
                }
  
                else if(btn[3].getText().equals("X") &amp;&amp; btn[5].getText().equals("O") &amp;&amp; btn[7].getText().equals("X")) {
                    btn[4].setText("O");
                    btn[4].setEnabled(false);
                }
  
                else if(btn[5].getText().equals("")){
                    btn[5].setText("O");
                    btn[5].setEnabled(false);
                }
  
                else if(btn[1].getText().equals("")){
                    btn[1].setText("O");
                    btn[1].setEnabled(false);
                }
                else {
                    if(count &gt;= 9)
                        checkWin();
                    else
                        RandomMove();
                }
  
                checkWin();
  
            }
  
            public void RandomMove(){
                Random x = new Random();
                int y = 1 + x.nextInt(9);
                if(btn[y].getText().equals("O") || btn[y].getText().equals("X") ){
                    RandomMove();
                } else {
                    btn[y].setText("O");
                    btn[y].setEnabled(false);
                }
            }
  
   public void checkWin(){
  
          /*Determine who won*/
          for(int i=0; i&lt;=7; i++){
              if( btn[winCombinations[i][0]].getText().equals(btn[winCombinations[i][1]].getText()) &amp;&amp;
                  btn[winCombinations[i][1]].getText().equals(btn[winCombinations[i][2]].getText()) &amp;&amp;
                  !btn[winCombinations[i][0]].getText().equals("")) {
                  win = true;
              }
          }
  
          if(count % 2 == 0)
              letter = "&lt;font color='blue'&gt;O";
          else
              letter = "&lt;font color='red'&gt;X";
  
  
  
          /*Shows Win &amp; Tie Status Message*/
          if(win == true){
              JOptionPane.showMessageDialog(null, letter + " WINS!&lt;/font&gt;&lt;/html&gt;");
          } else if(count == 9 &amp;&amp; win == false){
              JOptionPane.showMessageDialog(null, "Tie Game!");
          }
  
  }
      public static void main(String[] args){
          new TicTacToeGame();
      }
  }
  </code></pre>