A *相邻节点nullpointerexception

时间:2013-01-29 21:30:14

标签: 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;
    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; 
}
}

这是我的Node类

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&gt; -1&amp;&amp; node.y + y&gt; -1){

希望我没有咬过的东西比我能咬的更多。我正在尝试为节点周围的所有相邻节点做一个arraylist。

1 个答案:

答案 0 :(得分:1)

错误可能是你打电话:

currentNode = findBestNode(open);

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