0
    public void actionPerformed(ActionEvent e){
             grid=new JButton[length+20][width+20];
             grid1=new JButton[length+20][width+20];

            for(int i=0;i<length+2;i++)
            {
                for(int j=0;j<width+2;j++)
                {
                    grid1[i][j]=grid[i][j];
                }
            }


            for(int i=1;i<length+1;i++)
            {
                for(int j=1;j<width+1;j++)
                {
                    //final int row = i;
                    //final int col = j;

                    int count=0;

                    if(grid[i][j-1].getBackground() == Color.BLACK);
                        count++;

                    if(grid[i][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j-1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i+1][j-1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i+1][j].getBackground()==Color.BLACK)
                        count++;


                    if(grid[i+1][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(count==3)                    // exactly three neighbors
                    {

                        if(grid[i][j].getBackground()==Color.WHITE)
                        {
                            grid1[i][j].setBackground(Color.BLACK);         // birth cell
                        }
                    }

                    if(count==2 || count==3)            // 2 or 3 neighbors
                    {

                        if(grid[i][j].getBackground()==Color.BLACK)
                        {
                            grid1[i][j].setBackground(Color.BLACK);         // survives
                        }
                    }

                    if(count>=4 || count<=1)            //4 or more neighbors, or 1 or less neighbor
                    {
                        if(grid[i][j].getBackground()==Color.BLACK)
                        {
                            grid1[i][j].setBackground(Color.WHITE);         // dies from over-population or isolation
                        }
                    }
                }
            }

            for(int i=0;i<length+2;i++)
            {
                for(int j=0;j<width+2;j++)
                {
                    grid[i][j]=grid1[i][j];
                }
            }

            for(int i=1;i<length+1;i++)
            {
                for(int j=1;j<width+1;j++)
                {
                    System.out.print(grid[i][j]);
                }
                System.out.println("\n");
            }
        }

当我尝试使用 GUI 显示下一代康威生活游戏时,我收到了一个空指针异常。请建议我的代码有什么问题。当点击开始按钮时执行action方法

4

1 回答 1

2

NullPointerException 的原因是这样的:

grid  = new JButton[length+20][width+20];
grid1 = new JButton[length+20][width+20];

这样,您就有了 的 2D 数组JButtons,但它仍然充满了null值。您必须初始化数组中的各个“单元格”:

for (int i = 0; i < length+20; i++) {
    for(int j = 0; j < width+20; j++) {
        grid1[i][j] = new JButton();
    }
}

另外,数组的大小是有意的,还是应该是length+2x width+2,就像在你的 for 循环中一样?

但这不是您的实际问题:您创建一个新的按钮数组,然后检查这些新创建的按钮的背景颜色。假设它grid代表游戏的当前状态,您在进行更新之前正在擦除游戏状态。更有可能的是,您必须完全放弃grid = new JButton[length+20][width+20];

即使这样也不能正常工作,因为两个数组gridgrid1持有相同的按钮,所以当你改变一个的背景颜色时,你也改变了备份中的背景颜色。您只需grid1[i][j]=grid[i][j]将按钮的引用复制到另一个数组,但不要创建新按钮。即使你这样做了,你也会遇到这个新按钮根本不在 GUI 中的问题。

与其将游戏状态存储在 GUI 元素中,不如使用两个 2D 布尔数组(一个用于当前状态,一个作为状态更新期间前一个状态的备份)并设置按钮的背景颜色那些布尔值。

于 2013-10-25T08:21:01.583 回答