-2

I am trying to make my first tik tak toe game however I have ran into a problem that i have quiete some trouble sovling. When i try to press any button instead of disabling that button it says something Along the lines of Arrayindex out of bounds for Index 9 which i think is weird considering the fact that the Actionlistener should only be used 9 times where the variabe i starts at 0 and adds 1 every time it loops.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 9

at Fenster.lambda$0(Fenster.java:38)

    import java.awt.Color;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Fenster extends JFrame implements ActionListener{
    
    
        private JButton knapp1 = new JButton();
        private JButton knapp2 = new JButton();
        private JButton knapp3 = new JButton();
        private JButton knapp4 = new JButton();
        private JButton knapp5 = new JButton();
        private JButton knapp6 = new JButton();
        private JButton knapp7 = new JButton();
        private JButton knapp8 = new JButton();
        private JButton knapp9 = new JButton();
        
        private JButton knappar[] = {knapp1, knapp2, knapp3, knapp4, knapp5, knapp6, knapp7, knapp8, knapp9};
    
        private int i = 0;
        private int h = 10;
        private int b = 10;
    
        Fenster(){
        
                
            while(i <= 8) {
            
            knappar[i].setBounds(b, h, 100, 100);
            knappar[i].setLayout(null);
            knappar[i].addActionListener(e -> knappar[i].setEnabled(false));
            knappar[i].setBackground(Color.green);
            this.add(knappar[i]);
            
            if (i%2 == 0) {
            }
            
            b = b+110;
                
            if(b > 230) {
                b = 10;
                h =h+110;
            }
            i++;
    //      System.out.println(i);1
            }
                    
            JPanel brada = new JPanel();
            
            brada.setLayout(null);
            brada.setSize(340, 340);
            brada.setBackground(Color.black);
            
    
            this.add(brada);
            this.setLayout(null);
            this.setVisible(true);
            this.setSize(340, 440);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {    
        }   
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dava222
  • 11
  • 1

1 Answers1

0

Your while loop ends when i has the value 9. So when your action listener later runs e -> knappar[i].setEnabled(false), it is trying to access knapper[9]. Your i should not be a field, and you shouldn't be trying to use i, a loop variable, inside your action listener.

Instead you could set another variable to the object you need to reference and use that:

while (i <= 8) {
    final JButton btn = knapper[i];
    btn.setBounds(b, h, 100, 100);
    btn.setLayout(null);
    btn.addActionListener(e -> btn.setEnabled(false));
    btn.setBackground(Color.green);
    this.add(btn);
    ...
}
khelwood
  • 55,782
  • 14
  • 81
  • 108