я создаю лабиринт генератор в качестве побочного проекта для удовольствия, и я бегу на вопрос, гдеМой код возвращает нуль-то, я понятия не имею, почему
newTile.getxCoord()
создающей исключения нулевого указателя, и я понятия не имею, почему. Я проверил, чтобы создание моей «плитки» использовало положительные значения, которые существуют в пределах моей доски.
Ссылка на метод и код в целом https://github.com/Dibes/Maze/blob/master/src/MazeCreation/DepthFirst.java#L30.
Для быстрой ходьбы через мой мыслительный процесс для этого кода
// Set the starting tile as visited and current
TileGenerator.setTileVisible(Maze.startingTile.getxCoord(), Maze.startingTile.getyCoord(), 0);
// Set the starting tile as the current tile
TileGenerator.setTileCurrent(Maze.startingTile.getxCoord(), Maze.startingTile.getyCoord(), 1);
// Set the last tile as the starting tile
Tile lastTile = Maze.startingTile;
// Grab a new tile that is within the bounds of the map and is greater than 0
Tile newTile = TileGenerator.getTile(Utility.getHorizNext(lastTile.getxCoord(), -1, 1), Utility.getVertNext(lastTile.getyCoord(), -1, 1));
while(!TileGenerator.isTilesVisited()) {
// Debug testing
if (newTile.getxCoord() < 0 || newTile.getyCoord() < 0) {
System.out.println(newTile.getxCoord() + " " + newTile.getyCoord());
}
// Set the current tile visible
TileGenerator.setTileVisible(newTile.getxCoord(), newTile.getyCoord(), 0);
// Set the last tile marked as not current
TileGenerator.setTileCurrent(lastTile.getxCoord(), lastTile.getyCoord(), 0);
// Set the newly found tile marked as current (shows up as red on the board)
TileGenerator.setTileCurrent(newTile.getxCoord(), newTile.getyCoord(), 1);
// Grab new tile
lastTile = newTile;
newTile = TileGenerator.getTile(Utility.getHorizNext(lastTile.getxCoord(), -1, 1), Utility.getVertNext(lastTile.getyCoord(), -1, 1));
// A sleep in the thread so i could see the generation slowly
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Извините за то, что коротко, я просто не уверен, почему это было бы создать нулевой указатель.
Пожалуйста, разместите соответствующий код вместо публикации ссылки. – rgettman
Либо 'newTile' равно null, либо что-то под' getxCoord() 'равно null. Посмотрите на свою трассировку стека, чтобы определить, какой из них. – pamphlet
Вы не проверяете, возвращает ли 'getTile' значение NULL. Вы уверены, что это не может потерпеть неудачу? – usr2564301