-3
JButton btn = new JButton();
    JButton[][] boutons = {{btn},{btn}};

    public Fenetre() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridLayout(9, 9));

        setContentPane(contentPane);
        for (int ligne=1;ligne<=9;ligne++) {
            for(int colone=1;colone<=9;colone++) {
                //btn = new JButton();
                contentPane.add(boutons[ligne][colone]);
                btn.setName(String.valueOf(ligne) + "" + String.valueOf(colone));
                System.out.println(String.valueOf(ligne) + "" + String.valueOf(colone));
                btn.addActionListener(this);
            }
        }

What does my error come from ? I don't find the cause of my exception, please help me

java.lang.ArrayIndexOutOfBoundsException: 1

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

You are indexing boutons out of bounds.

boutons is two dimensional array of size 2x1 so only valid indices are:

boutons[0][0] and boutons[1][0]

and you iterate [1..9][1..9] for boutons[ligne][colone]

Pavol Loffay
  • 241
  • 3
  • 8
0

You are doing this:

 for (int ligne=1;ligne<=9;ligne++) {

in an array that is not containing so much elements..

 JButton[][] boutons = {{btn},{btn}};

you need to loop in the for according to the lenght of the boutons array..

JButton[][] boutons = {{btn},{btn}};
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97