-3

Trying to do some A* stuff and I'm constantly getting a NullPointerException at line 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; 
}
}

Here's my Node class

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;
}
}

and the game class:

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();

}
}

Thanks in advance for any help!

Line 129 is in

ArrayList getAdj(Node node){

The exact line is:

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

Hope I didn't bite off more than I could chew. I'm trying to make an arraylist of all the adjacent Nodes surrounding the node.

Daniel Gratzer
  • 49,751
  • 11
  • 87
  • 127
eee
  • 53
  • 5
  • I've tried rewriting the code for getAdj about 4 or 5 times now and I'm getting the same result. I don't understand why it's happening. From what I understand it's saying there is no value at node.x or y value at node.y but there is and the code works if I don't add anything to the adj arraylist. – eee Jan 29 '13 at 21:38
  • It's not the x or y. `node` is null.. *Hint primitives can't throw NullPointers* Check out http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception – Daniel Gratzer Jan 29 '13 at 21:40
  • what I don't understand is how node could possibly be null... it's not like I change the node whatsoever and the code works up to a point, here's the output when I run it (shows that node is working but suddenly is null) `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.(Game.java:11) at ericm.game.Game.main(Game.java:16)` – eee Jan 29 '13 at 21:42
  • So basically it knows which nodes surround it but it won't add them to the arraylist once it's exited the for loop – eee Jan 29 '13 at 21:45
  • What happens if there are no nodes open? – Daniel Gratzer Jan 29 '13 at 21:47
  • I looked through it, and I can't find where the problem is, but you really have to use the debugger or something to get a better idea of what's happening. It's really difficult for us here to just look at the code and find the problem – Sam I am says Reinstate Monica Jan 29 '13 at 21:49

1 Answers1

1

The error probably is that you call:

currentNode = findBestNode(open);

This returns null, if open is empty. I'm not sure, if this can be possible, but if so, you call getAdj(null).
And that leads to a NullPointerException..

MalaKa
  • 3,574
  • 2
  • 16
  • 31