0

我真的不知道是什麼導致了這個問題,但我的程序應該是康威的生命遊戲,在2代後崩潰,看起來不管我做什麼,而且我我一直在努力尋找錯誤。2D數組和空指針異常(Java)

我把事業縮小到了一些可能的領域 - 或者至少我認爲我有。

short numNeighbors(int x, int y) { 
    short numNeighbors; 
    numNeighbors = 0; 
    if(x > 0 && y > 0 && matrix[x][y] != null){ 
     if (matrix[x+1][y] == true) numNeighbors++; 
     if (matrix[x][y+1] == true) numNeighbors++; 
     if (matrix[x+1][y+1] == true) numNeighbors++; 
     if (matrix[x][y-1] == true) numNeighbors++; 
     if (matrix[x-1][y] == true) numNeighbors++; 
     if (matrix[x+1][y-1] == true) numNeighbors++; 
     if (matrix[x-1][y+1] == true) numNeighbors++; 
     if (matrix[x-1][y-1] == true) numNeighbors++; 
    } 
    return numNeighbors; 
} 
//returns the number of neighbours that a coordinate has 

我假定這部分我上面的二維數組的邊界以外的檢查,但不應該是可能的,因爲我花了預防措施,以確保沒有發生。即便如此,這是一個可能的原因。

void nextGen(){ 
    Boolean[][] newMatrix = new Boolean[rows()][cols()]; 

    for (int i = 1; i < cols()-1; i++){ 
     for (int j = 1; j < rows()-1; j++){ 
     //avoiding null pointer errors 
      if (matrix[j][i] == null) matrix[j][i] = false; 
      //if a cell has 3 neighbours, become or stay true 
      if (numNeighbors(j, i) == 3) newMatrix[j][i] = true; 
      //if it doesn't have 3 neighbours, become or stay false 
      else newMatrix[j][i] = false; 
     } 
    } 

    matrix = newMatrix; 
} 
//makes matrix represent the next generation 

這是我對錯誤原因的下一次猜測,但我無法真正知道會出現什麼錯誤。

for (int j = 0; j < numGenerations; j++){ 
     JOptionPane.showMessageDialog(null,"generation " + (j+1) + ":\n\n" + myGrid.showGrid()); 
     myGrid.nextGen(); 
    } 

我只是發佈上面,因爲它調用上面的塊,我不想排除任何東西。

我真的不知道還有什麼問題可以解決,但如果有人想看看我項目的完整源代碼,我已經發布了它on pastebin

+0

什麼行號是堆棧跟蹤中發生的NullPointerException?這將是一個很好的起點。你嘗試過調試嗎?在發生NullPointerException的行之前放入幾個斷點並檢查數組值。 – 2012-01-07 22:54:15

+1

你可以發佈你的錯誤stacktrace? – 2012-01-07 22:54:21

回答

2

在次世代你這樣做:

//avoiding null pointer errors 
if (matrix[j][i] == null) matrix[j][i] = false; 

執行相同的所有IFS在numNeighbors()

short numNeighbors(int x, int y) { 
    short numNeighbors; 
    numNeighbors = 0; 
    if(x > 0 && y > 0 && matrix[x][y] != null){ 
     if (matrix[j][i] != null && matrix[x+1][y] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x][y+1] == true) numNeighbors++; 
     if (matrix[j][i] != null && [x+1][y+1] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x][y-1] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x-1][y] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x+1][y-1] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x-1][y+1] == true) numNeighbors++; 
     if (matrix[j][i] != null && matrix[x-1][y-1] == true) numNeighbors++; 
    } 
    return numNeighbors; 
} 

甚至更​​好提前進行實例化的所有單元格設置爲false。

//Run in constructor 
for(int i .. 
    for(int j .. 
     matrix[j][i] = false 
+3

我喜歡關於預先實例化所有單元格的最後部分。 1 + – 2012-01-07 23:06:36

+0

我只是試過這個,它工作,但它與newMatrix,而不是矩陣。爲了安全起見,我讓矩陣的構造函數爲假,但我認爲布爾運算符在Java中默認爲false,但顯然它們是空的。儘管如此,我仍然不知道爲什麼它能夠工作兩代。 – Megafonzie 2012-01-07 23:37:33

+0

它的劑量看到我的新答案 – Farmor 2012-01-07 23:57:38

1

實際上,全部塊應該用大括號括起來。如果你花時間做這個,你會多次保存你的尾巴。例如,

if (matrix[j][i] == null) { 
    newMatrix[j][i] = false; 
} 

編輯2
你大,如果塊都將有邊界的問題。爲什麼不直接使用嵌套的for循環:

short numNeighbors(int x, int y) { 
    short numNeighbors; 
    numNeighbors = 0; 

    int xMin = Math.max(x - 1, 0); 
    int xMax = Math.min(x + 1, MAX_X - 1); // MAX_X is a constant, number of columns 
    int yMin = Math.max(y - 1, 0); 
    int yMax = Math.min(y + 1, MAX_Y - 1); // ditto, number of rows 

    for (int i = xMin; i <= xMax; i++) { 
    for (int j = yMin; j <= yMax; j++) { 
     if (i != x && j != y) { 
      if (matrix[i][j]) { 
       numNeighbors++; 
      } 
     } 
    } 
    } 

    return numNeighbors; 
} 

與其他地方一樣,在我的評論中提到,數組應該初始化爲非空值,所以應該不需要空檢查。

+0

這是一個邏輯問題,但我不會產生NPE。 – Farmor 2012-01-07 22:56:11

+0

我這樣做是爲了檢查矩陣[] []中可能的空值。我對我的代碼進行了類似的檢查,但他們似乎沒有幫助。我也嘗試改變它,並沒有透露額外的信息。我仍然得到同樣的錯誤,並在兩代之後出現同樣的崩潰。 – Megafonzie 2012-01-07 23:02:33

+0

我的意見是針對氣墊船Full Of Eels編輯回答 – Farmor 2012-01-07 23:04:45

0

我會在檢查您的整個項目時發佈另一個答案。

void nextGen(){ 
    Boolean[][] newMatrix = new Boolean[rows()][cols()]; 

你做什麼其中創建布爾對象的數組,而不是布爾元。

  • 布爾元默認爲false
  • 布爾對象默認爲空

Java有一種叫做自動裝箱原語可以是棘手和隱藏這種「SMaL公司」差異這實際上可以是真正意義上的,如在這裏看到的,