-3

いくつかのA*のことをしようとすると、私は常にNullPointerException129行目で取得しています...

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){

噛むことができる以上に噛まなかったといいのですが。ノードを囲むすべての隣接ノードの配列リストを作成しようとしています。

4

1 に答える 1

1

エラーはおそらくあなたが呼び出すことです:

currentNode = findBestNode(open);

空の場合、これはnullを返しますopen。これが可能かどうかはわかりませんが、可能であれば、に電話してくださいgetAdj(null)
そして、それはNullPointerExceptionにつながります。

于 2013-01-29T22:25:58.260 に答える