2013-10-25 3 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 game of life를 표시하려고 할 때 nullpointer 예외가 발생합니다. 제 코드에 뭐가 잘못 됐는지 제안 해주세요. 동작 수행 방법은 시작 버튼이 NullPointerException이의 원인이있다guiw와 conways 코드 결합

+0

NullPointerException - 어떤 행에 있습니까? –

+2

예외 추적을 공유하십시오. –

+0

1) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 2) 항상 오류 및 예외 출력을 복사/붙여 넣기하십시오. –

답변

2

을 클릭하면 실행됩니다

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

이 방법, 당신은 JButtons의 2 차원 배열을 가지고 있지만 여전히 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해야 하는가?

그러나 실제 문제는 아닙니다. 새 단추 배열을 만든 다음 새로 만든 단추의 배경색을 확인합니다. grid이 게임의 현재 상태를 나타내는 것으로 가정하면 업데이트를 수행하기 전에 게임 상태를 지우기 이됩니다. 더 많은 경우, grid = new JButton[length+20][width+20]; 행을 완전히 삭제해야합니다.

그리고 심지어이 제대로 작동하지 않습니다

, 두 배열 gridgrid1같은 버튼을 개최한다, 그래서 당신은 하나의 배경 색상을 변경할 때, 당신은 또한 백업에 배경색을 변경한다. grid1[i][j]=grid[i][j]을 사용하면 단추에 대한 참조를 다른 배열로 복사하지만 새 단추를 만들지는 않습니다. 그리고 그랬더라도 새로운 버튼이 GUI에 전혀 없을 것입니다.

GUI 요소에 게임 상태를 저장하는 대신 두 개의 2 차원 불리언 값 배열 (현재 상태에 하나, 상태 업데이트 중에 이전 상태의 백업으로 하나)을 사용하고 그 불리언에 근거한 버튼.