此代码是我使用 Java Swing 制作的井字游戏程序的一部分。添加按钮的for语句为什么会返回NullPointerException?
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToeGui extends JFrame
{
public final static int r = 3;
public final static int c = 3;
TicTacToeGui()
{
JButton[][] button = new JButton[3][3];
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(r, c));
JLabel label = new JLabel("This is a tic tac toe game.");
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
panel.add(button[i][j]);
}
}
this.add(label);
this.add(panel);
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String [] args)
{
new TicTacToeGui();
}
}