我有一个数组初始化和填充,但我不能在其填充的任何其他方法中访问它的值.如果我尝试在其他方法中使用它我得到nullPointerException.例如,如果我在init方法中说"System.out.println(board [2] [2])"它可以工作,但在另一个方法中抛出nullPointerException.我已经花了好几个小时才搞清楚,但不知道出了什么问题?任何人都可以对我的问题有所了解吗?非常感谢.谢谢
public class Program2 extends JPanel implements ActionListener
{
private LifeCell[][] board;
private JButton next;
private JFrame frame;
public static void main(String[] args) {new Program2();}
public Program2()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(this);
this.init();
frame.pack();
frame.setVisible(true);
}
public void init()
{
LifeCell[][] board = new LifeCell[10][10];
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
board[r][c] = new LifeCell(board, r, c);
this.add(board[r][c]);
board[r][c].setBounds(x,y,40,40);
this.setVisible(true);
System.out.println(board[2][2]) //works …Run Code Online (Sandbox Code Playgroud)