-3

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

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

确切的行是:

如果(节点.x+x>-1 && 节点.y+y>-1){

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

4

1 回答 1

1

错误可能是您调用:

currentNode = findBestNode(open);

如果open为空,则返回 null。我不确定,如果这可能,但如果是这样,你打电话给getAdj(null).
这会导致 NullPointerException..

于 2013-01-29T22:25:58.260 回答