0

この問題の原因はわかりませんが、私のプログラムはConwayのGame of Lifeであるはずですが、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 

私は2次元配列の境界外のチェック以上、このセクションと仮定していますが、私はそれが実現しなかったことを確認するために予防措置を取ったので、それは、可能ではありません。それでも、これが原因の1つです。

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が発生する行の前にいくつかのブレークポイントを置き、配列の値を調べます。 –

+1

エラーのスタックトレースを投稿できますか? –

答えて

2

あなたが実行します。

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

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にすべてのセルを事前にインスタンス内のすべてのIFSのために同じことを行います。

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

私はすべてのセルを事前インスタンス化する最後の部分が好きです。 1 + –

+0

私はこれを試してみましたが効果はありましたが、マトリックスではなくnewMatrixを使っていました。私は行列のコンストラクタを安全のためにfalseにしていましたが、ブーリアンはJavaでfalseにデフォルト設定されていましたが、明らかにnullを開始します。私はまだそれが2世代の間働く理由はまだ分かりません。 – Megafonzie

+0

私の新しい答えが見えます – Farmor

1

実際には、すべてブロックを中括弧で囲む必要があります。あなたがこれをする時間がかかるなら、あなたはあなたの尾を何度も救うでしょう。例えば、

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

編集2
あなたの大きなブロックは、境界問題を持っているとしている場合。なぜ単純にループのためにネストされて使用していない:

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; 
} 

そして他の場所で言及し、nullチェックは必要ないはずですので、私のコメントで、配列がnull以外の値に初期化されるべきです。ネクストジェンは

+0

これはロジックの問題ですが、NPEは生成しません。 – Farmor

+0

私は行列[] []で可能なヌル値をチェックするためにそれをやっていました。私は自分のコードの周りに同様の小切手を置いたが、彼らは役に立たないようだ。私もそれを変更しようとしました、そして、余分な情報は明らかにされませんでした。私はまだ同じエラーと2世代後の同じクラッシュを取得します。 – Megafonzie

+0

私のコメントは、ホバークラフト完全版Eelsの編集の回答 – Farmor

0

私はあなたのプロジェクト全体を検査しましたので、別の回答を投稿します。

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

あなたは基本論理要素をブールオブジェクトの配列を作成していないところ何。偽

  • ブールに

    • 基本論理要素のデフォルトはnullにデフォルトをオブジェクト

    Javaが難しいことや、「SmaIで」違いのこの種を非表示にすることができますプリミティブのためにオートボクシングと呼ばれるものを持っていますここに見られるように本質的に重要なのは