this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
why getting nullpointerexception?? instantiate node object in board.setstartposition. call driver using b.getstartposition(). have simplified code 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 cant find path or exhaust our searches //this nullpointer. seems think node //n null, eventhough when pass it, pass getting startposition node //add startposition closed list closedlist.add(n); //add things around start square openlist if applicable addtoopenlist(n); } }
you never initialized closedlist or openlist. change
private arraylist<node> openlist; private arraylist<node> closedlist; to like
private arraylist<node> openlist = new arraylist<>(); private arraylist<node> closedlist = new arraylist<>(); or, better yet, suggest program list interface. like,
private list<node> openlist = new arraylist<>(); private list<node> closedlist = new arraylist<>();
Comments
Post a Comment