I have an array initialized and filled up, but I can not access its values in any other methods than the one it is filled in. If I try to work with it in other methods I get a nullPointerException. For example if I say "System.out.println(board[2][2])" inside the init methos it works but in another method throws nullPointerException. I have been trying for hours to figure it out but have no idea what's wrong? Can anybody shine some light on my problem? It would be much appreciated. Thanks
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
}
}
}
public void test(){ System.out.println(board[2][2])}//doesn't work
}