java判断二维数组是否空,二维数组和空指针异常(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我假设上面的这一部分检查了我的2D阵列的边界之外,但这不应该是可能的,因为我采取了预防措施以确保没有发生。即便如此,这是一个可能的原因。

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 doesnt 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) + ": " + myGrid.showGrid());

myGrid.nextGen();

}我只发布上面的内容,因为它会调用上面的块,我不想排除任何内容。

我真的不知道还有什么问题,但为了防止任何人想看看我的项目的完整源代码,我已经发布了on pastebin。

我真的不知道是什么原因导致了这个问题,但是我的程序应该是康威的生命游戏,在两代之后崩溃,看起来不管我做了什么,而且我一直在努力寻找错误。 我将事业范围缩小到几个可能的领域 - 或者至少我认为我有。 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我假设上面的这一部分检查了我的2D阵列的边界之外,但这不应该是可能的,因为我采取了预防措施以确保没有发生。即便如此,这是一个可能的原因。 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 doesnt 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) + ": " + myGrid.showGrid()); myGrid.nextGen(); }我只发布上面的内容,因为它会调用上面的块,我不想排除任何内容。 我真的不知道还有什么问题,但为了防止任何人想看看我的项目的完整源代码,我已经发布了on pastebin。
经验分享 程序员 微信小程序 职场和发展