Java A*相邻节点nullpointerexception

Java A*相邻节点nullpointerexception,java,arraylist,nullpointerexception,a-star,Java,Arraylist,Nullpointerexception,A Star,尝试做一些A*的事情,我经常在第129行得到一个NullPointerException public class GameMap { int tileW = 64; int tileH = 36; int tiles = 2304; ArrayList<Node> nodes = new ArrayList<Node>(); public GameMap(int width, int height) { int widthtest = 640; i

尝试做一些A*的事情,我经常在第129行得到一个NullPointerException

public class GameMap {

int tileW = 64;
int tileH = 36;
int tiles = 2304;
ArrayList<Node> nodes = new ArrayList<Node>();

public GameMap(int width, int height) {

    int widthtest = 640;
    int heighttest = 360;

    for (int y = 0; y<tileH; y++){

        for (int x = 0; x<tileW; x++){

            nodes.add(new Node(x,y,false,null,10000));
            //System.out.println("node added at x:" + x + " and y:" + y + " w/tilenum: " + nodes.size());
        }

    }

}

public void blockNode(int x, int y){
    int tilenum = tileNum(x,y);
    nodes.get(tilenum).blocked = true;
    System.out.println("blocked node " + tilenum);
}

public int tileNum(int x, int y){
    int tilenum = x+(y*64);
    return tilenum;
}

public ArrayList<Node> findPath(int sx, int sy, int tx, int ty){
    ArrayList<Node> open = new ArrayList<Node>();
    ArrayList<Node> closed = new ArrayList<Node>();
    ArrayList<Node> path = new ArrayList<Node>();
    ArrayList<Node> adjList = new ArrayList<Node>();
    Node startNode = new Node(sx, sy, false, null, getF(sx,sy,tx,ty));
    Node endNode = new Node(tx, ty, false, null, 0);
    Node currentNode = null;
    open.add(startNode);
    while(currentNode != endNode && open.size()>0 && endNode.parent == null){
        currentNode = findBestNode(open);
        adjList = getAdj(currentNode);
        if(currentNode.equals(endNode)){
            continue;
        }
        else{
            open.remove(currentNode);
            closed.add(currentNode);
            for(int i = 0 ; i<adjList.size() ; i++){

                if(adjList.get(i).blocked || closed.contains(adjList.get(i))){
                    continue;
                }

                if(!open.contains(adjList.get(i))){
                    open.add(adjList.get(i));
                    adjList.get(i).parent = currentNode;
                }

                else{

                    int newDistance = getF(currentNode.x, currentNode.y, tx, ty);
                    if(newDistance < currentNode.fscore){
                        adjList.get(i).parent = currentNode;
                        adjList.get(i).fscore = newDistance;
                    }

                }

            }

        }

    }

    if(endNode.parent != null){
        path = generatePath(startNode, endNode);
    }


    return path;
}

private ArrayList<Node> generatePath(Node startNode, Node endNode) {
    ArrayList<Node> path = new ArrayList<Node>();
    Node currentNode = endNode;
    while(currentNode != startNode){
        path.add(currentNode);
        currentNode = currentNode.parent;
    }

    return path;
}

private Node findBestNode(ArrayList<Node> open) {
    int lowF = 10000;
    Node currentNode = null;
    for (int i = 0 ; i<open.size() ; i++){
        if(open.get(i).fscore < lowF){
            lowF = open.get(i).fscore;
            currentNode = open.get(i);
        }
    }
    return currentNode;
}

private int getF(int sx, int sy, int tx, int ty){
    int dx = Math.abs(tx - sx);
    int dy = Math.abs(ty - sy);
    int f = dx+dy;
    return f;
}

private ArrayList<Node> getAdj(Node node){
    ArrayList<Node> adj;
    adj = new ArrayList<Node>();
    for (int y = -1; y<2; y++){
        for (int x = -1; x<2; x++){

            if(node.x+x>-1 && node.y+y>-1){
                Node theNode = nodes.get(tileNum(node.x+x, node.y+y));
                //adj.add(theNode);
                System.out.println(theNode);
                adj.add(theNode);
            }

        }

    }
    return adj; 
}
}
游戏类:

public class Game {

public Game() {
    GameMap gm = new GameMap(640,360);
    gm.blockNode(63, 32);

    System.out.println(gm.findPath(0, 0, 4, 2));
}

public static void main(String[] args){

    Game game = new Game();

}
}
提前感谢您的帮助

第129行在

ArrayList getAdjNode节点{

确切的路线是:

ifnode.x+x>-1&&node.y+y>-1{


希望我没有咬得太多。我正在尝试创建节点周围所有相邻节点的数组列表。

错误可能是您调用:

currentNode = findBestNode(open);
如果open为空,则返回null。我不确定这是否可能,但如果可能,则调用getAdjnull。
这导致了一个NullPointerException..

我已经尝试重写getAdj的代码4到5次了,我得到了相同的结果。我不明白为什么会发生这种情况。据我所知,node.x没有值,node.y没有值,但如果我不向adj arraylist添加任何内容,代码就会工作。它是nx或y节点为空..提示原语不能抛出空指针检查我不明白的是节点如何可能为空..这不像我改变了节点,代码工作到某一点,这里是我运行时的输出,它显示节点正在工作,但突然被空阻止节点2111 ericm.map。Node@e80317埃里克地图.Node@22e3ac埃里克·梅普。Node@916ab8埃里克·梅普。Node@f55759线程主java.lang.NullPointerException位于ericm.map.GameMap.getAdjGameMap.java:129位于ericm.map.GameMap.findPathGameMap.java:51位于ericm.game.game.game.java:11位于ericm.game.game.mainGame.java:16所以它基本上知道它周围的节点,但不会将它们添加到rraylist一旦退出for循环,如果没有打开的节点会发生什么?谢谢!我不得不调整一些事情,我终于让它工作了!
currentNode = findBestNode(open);