2013-10-25 34 views
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顯示下一代conway遊戲時,出現空指針異常。請建議我的代碼有什麼問題。啓動按鈕被點擊將conway代碼與gui結合使用

+0

NullPointerException在哪一行? –

+2

請分享異常追蹤。 –

+0

1)爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 2)始終複製/粘貼錯誤和異常輸出。 –

回答

2

當NullPointerException異常的原因是,這是執行該操作執行的方法:

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

這樣一來,你的JButtons一個二維數組,但它仍然是充滿null值。你有數組初始化各個「細胞」:

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

而且,是數組的大小是故意的,還是應length+2 X width+2相反,在你的for循環?

但這不是你的實際問題:你創建一個新的按鈕數組,然後檢查那些新創建的按鈕的背景顏色。假設grid代表遊戲的當前狀態,那麼在更新之前您將擦除遊戲狀態。更可能的是,你必須完全放棄grid = new JButton[length+20][width+20];

即使這樣也無法正常工作,因爲這兩個陣列gridgrid1將舉行相同按鈕,所以當你改變一個的背景顏色,你也改變了備份的背景色。用grid1[i][j]=grid[i][j]您只需將該按鈕的引用複製到另一個數組,但不要創建一個新按鈕。即使你這樣做了,你也會遇到新的按鈕根本不在GUI中的問題。

而不是將您的遊戲狀態存儲在GUI元素中,您應該使用兩個布爾二維數組(一個用於當前狀態,一個用作狀態更新期間之前狀態的備份),並將背景顏色設置爲基於那些布爾值的按鈕。

相關問題