本当に質問することはあまりありません。一体、私はその質問が何を言うべきかさえ知りません。基本的に、このJavaコードはコンパイルエラーなしで正常に動作します。
public class Application {
static String[][] tiles;
public Application() {
tiles = new String[9][9];
}
public static void main(String[] args) {
Application app = new Application();
Grid mines = new Grid();
mines.fillTiles(tiles, 9, 9, 10);
}
}
class Grid {
Random rand;
public void fillTiles(String[][] tiles, int rowSize, int colSize,
int numMines) {
rand = new Random();
int rowIndex;
int columnIndex;
while (numMines > 0) {
rowIndex = rand.nextInt(rowSize);
columnIndex = rand.nextInt(colSize);
tiles[rowIndex][columnIndex] = "M";
numMines--;
}
}
}
しかし、私が行を削除すると
Application app = new Application();
最初のクラスのメインメソッドから、次の場所でNullPointerExceptionをスローしています。
tiles[rowIndex][columnIndex] = new String("M");
理由は何ですか?