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