【问题标题】:A* Adjacent Nodes nullpointerexception [closed]A *相邻节点空指针异常[关闭]
【发布时间】:2013-01-29 21:30:14
【问题描述】:

尝试做一些 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 Node{

int x;
int y;
boolean blocked;
Node parent;
int fscore;

public Node(int x, int y, boolean blocked, Node parent, int fscore) {
    this.x = x;
    this.y = y;
    this.blocked = blocked;
    this.parent = parent;
    this.fscore = fscore;
}
}

和游戏类:

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 getAdj(节点节点){

确切的行是:

if(node.x+x>-1 && node.y+y>-1){

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

【问题讨论】:

  • 我已经尝试为 getAdj 重写代码大约 4 或 5 次,我得到了相同的结果。我不明白为什么会这样。据我了解,它是说 node.x 上没有值或 node.y 上的 y 值,但是如果我不向 adj 数组列表添加任何东西,代码就可以工作。
  • 不是 x 或 y。 node 为 null.. 提示原语不能抛出 NullPointers 查看 stackoverflow.com/questions/218384/…
  • 我不明白的是节点如何可能为空......这不像我改变节点并且代码工作到一定程度,这是我运行它时的输出(显示该节点正在工作但突然为空)blocked node 2111 ericm.map.Node@e80317 ericm.map.Node@22e3ac ericm.map.Node@916ab8 ericm.map.Node@f55759 Exception in thread "main" java.lang.NullPointerException at ericm.map.GameMap.getAdj(GameMap.java:129) at ericm.map.GameMap.findPath(GameMap.java:51) at ericm.game.Game.&lt;init&gt;(Game.java:11) at ericm.game.Game.main(Game.java:16)
  • 所以它基本上知道它周围有哪些节点,但是一旦退出 for 循环,它就不会将它们添加到数组列表中
  • 如果没有节点打开会怎样?

标签: java arraylist nullpointerexception a-star


【解决方案1】:

错误可能是你调用:

currentNode = findBestNode(open);

如果 open 为空,则返回 null。我不确定这是否可行,但如果可以,请致电getAdj(null)
这会导致 NullPointerException..

【讨论】:

  • 谢谢!不得不调整一些东西,我终于让它工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
相关资源
最近更新 更多