我創建一個迷宮發電機作爲一個有趣的側面項目,我遇到了在那裏我的代碼以某種方式返回null,我不知道爲什麼
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()'下的內容爲空。看看你的堆棧跟蹤來確定哪個。 – pamphlet
你不檢查'getTile'是否返回NULL。你確定它不能失敗? – usr2564301