Java 我的代码以某种方式返回null,我不知道为什么

Java 我的代码以某种方式返回null,我不知道为什么,java,nullpointerexception,Java,Nullpointerexception,我正在创建一个迷宫生成器作为一个有趣的辅助项目,我遇到了一个问题 newTile.getxCoord() 正在创建一个空指针异常,我不知道为什么。我进行了检查,以确保我的“磁贴”的创建使用了存在于我的电路板范围内的正值 这里是指向整个方法和代码的链接 快速浏览我对这段代码的思考过程 // Set the starting tile as visited and current TileGenerator.setTileVisible(Maze.startingTile.getxCoor

我正在创建一个迷宫生成器作为一个有趣的辅助项目,我遇到了一个问题

newTile.getxCoord()
正在创建一个空指针异常,我不知道为什么。我进行了检查,以确保我的“磁贴”的创建使用了存在于我的电路板范围内的正值

这里是指向整个方法和代码的链接

快速浏览我对这段代码的思考过程

// 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();
        }
    }
//将起始磁贴设置为已访问和当前磁贴
TileGenerator.setTileVisible(Maze.startingTile.getxCoord(),Maze.startingTile.getyCoord(),0);
//将起始磁贴设置为当前磁贴
TileGenerator.setTileCurrent(Maze.startingTile.getxCoord(),Maze.startingTile.getyCoord(),1);
//将最后一个磁贴设置为起始磁贴
瓷砖lastTile=Maze.startingTile;
//抓取位于贴图边界内且大于0的新平铺
Tile newTile=TileGenerator.getTile(Utility.getHorizNext(lastile.getxCoord(),-1,1),Utility.getVertNext(lastile.getyCoord(),-1,1));
而(!TileGenerator.isTilesVisited()){
//调试测试
if(newfile.getxCoord()<0 | | newfile.getyCoord()<0){
System.out.println(newfile.getxCoord()+“”+newfile.getyCoord());
}
//将当前平铺设置为可见
TileGenerator.setTileVisible(newfile.getxCoord(),newfile.getyCoord(),0);
//将标记为非当前的最后一个磁贴设置为非当前磁贴
TileGenerator.setTileCurrent(lastTile.getxCoord(),lastTile.getyCoord(),0);
//将新找到的磁贴设置为当前磁贴(在板上显示为红色)
TileGenerator.setTileCurrent(newfile.getxCoord(),newfile.getyCoord(),1);
//抓取新瓷砖
lastTile=新文件;
newTile=TileGenerator.getTile(Utility.getHorizNext(lastTile.getxCoord(),-1,1),Utility.getVertNext(lastTile.getyCoord(),-1,1));
//睡在线里,这样我才能慢慢地看到这一代人
试一试{
睡眠(200);
}捕捉(中断异常e){
e、 printStackTrace();
}
}

很抱歉说得这么简单,我只是不知道为什么会创建空指针。

newfile
是由
TileGenerator.getTile()
创建的-根据
TileGenerator
的源代码,此方法可以返回空实例:


您的代码正在生成一个
NullPointerException
,因为该磁贴为空-您是否编写了一个单元测试来涵盖您正在使用的情况?

请发布相关代码,而不是发布链接。
newfile
为空,或者
getxCoord()
下的内容为空。查看堆栈跟踪以确定哪个堆栈跟踪。不检查
getTile
是否返回NULL。你确定它不会失败吗?@Jongware我完全没有对它做空检查>>谢谢!这正是问题所在!谢谢我会尽快把它作为答案。比发现bug更重要的是,通过一套好的单元测试,确保以后不会再发生错误。您可能还想重新访问
TileGenerator
中的
getTile
方法。返回
null
作为回退值有意义吗?异常在这里更有意义吗?我认为null在这种情况下是有意义的,因为它无法在该点到达磁贴,所以磁贴为null。如果我抛出一个异常,它会中断程序流程,我宁愿继续并更正坐标,而不是完全停止它。但我同意,当涉及到节目的其余部分时,我应该更多地考虑这个问题!
public static Tile getTile(int xCoord, int yCoord) {
    for (int[] tile : mapTiles) {
        if (tile[Tile.XCOORD] == xCoord && tile[Tile.YCOORD] == yCoord) {
            return new Tile(tile[Tile.XCOORD], tile[Tile.YCOORD], tile[Tile.XLOC], tile[Tile.YLOC], tile[Tile.ISFILLED], tile[Tile.ISVISITED], tile[Tile.ISCURRENT]);
        }
    }
    return null;
}