-2

Why am I getting a NullPointerException?? I instantiate the node object in the Board.setStartPosition. Then I call it from my driver using b.getStartPosition(). I have simplified my code to only leave relavant information.

Driver.java

Static Board b;

public static void main(String[] args){
     b = new Board(3,3);
     b.setStartPosition(1, 1);
     b.createPath(b.getStartPosition());
}

Board.java

public class Board {
    private int length;
    private int width;
    private Node[][] board;
    private ArrayList<Node> openList;
    private ArrayList<Node> closedList;
    private Node startPosition;
    private Node endPosition;

    Board(int length, int width){
        board = new Node[length][width];

        Random rand = new Random();

        for(int row=0; row < board.length; row++){
            for(int col=0; col < board[row].length; col++){

                int randomNum = rand.nextInt(10);

                if(randomNum == 0){
                    board[row][col] = new Node('X',row,col);
                }
                else{
                    board[row][col] = new Node('O',row,col);
                }
            }
        }
    }

    public Node getStartPosition() {
        return this.startPosition;
    }

    public void setStartPosition(int x, int y) {
        board[x][y].setStatus('S');
        board[x][y].setParent(null);
        startPosition = new Node('S', x, y);
        startPosition.setG(0);
    }

    public void createPath(Node n){

        //Continue until we cant find a path or we exhaust our searches

        //THIS IS WHERE I GET A NULLPOINTER. IT SEEMS TO THINK THAT MY NODE
        //n is null, eventhough when i pass it, i pass it by getting the startposition node
        //Add startPosition to closed list
        closedList.add(n);

        //Add things around start square to openList if applicable
        addToOpenList(n);       
    }

}
maasg
  • 37,100
  • 11
  • 88
  • 115
grauddi
  • 85
  • 1
  • 9

3 Answers3

1

You have not instantiated your openList & closedList. You have just declared them :

private ArrayList<Node> openList;
private ArrayList<Node> closedList;

hence when you do this

        closedList.add(n);

        //Add things around start square to openList if applicable
        addToOpenList(n);  

you will get NullPointerException.

You can initialise your lists as mentioned here at declaration:

 private ArrayList<Node> openList = new ArrayList<>();
 private ArrayList<Node> closedList = new ArrayList<>();
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Thanks . I should know this by know... I was debugging for like 30 minutes and could not figure it out. I figured it had to be something easy – grauddi Jul 21 '15 at 02:39
1

You never seem to set closedList. Why would you expect it NOT to be null?

John3136
  • 28,809
  • 4
  • 51
  • 69
1

You never initialized closedList or openList. Change

private ArrayList<Node> openList;
private ArrayList<Node> closedList;

to something like

private ArrayList<Node> openList = new ArrayList<>();
private ArrayList<Node> closedList = new ArrayList<>();

or, better yet, I suggest you program to the List interface. So really something like,

private List<Node> openList = new ArrayList<>();
private List<Node> closedList = new ArrayList<>();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249