Possible Duplicate:
objects classes and arrays – why is it returning ‘null’ ? [java]
The other questions with similar titles all have the answer that their data needs to be initialized which I have done but I’m still getting a null pointer exception. Could anyone tell me why?
public class grid{
private Node [][] board = new Node [9][9];
public boolean add(int x, int y, char label) {
boolean valid=true;
System.out.println("enter add");
if(label==' '){
System.out.println("enter if 1");
board[x][y].setValue('0');
}
else if(label<='9'&&label>'0'){
System.out.println("enter if 2");
board[x][y].setValue(label);
}
else{
valid=false;
}
if(valid)
System.out.println("valid");
return valid;
}
I’m getting the error on the setValue Lines (10 and 14)
public class Node{
public char value;
public char [] possibleValues = {1,2,3,4,5,6,7,8,9};
public boolean correct=false;
}
Edit: I figured it out, if anyone else has the same problem, this seems to fix it.
if(label==' '){
System.out.println("enter if 1");
board[x][y]= new Node(' ');
}
else if(label<='9'&&label>'0'){
System.out.println("enter if 2");
board[x][y]= new Node(label);
}
An array does not initialize the elements of the array. So, each
board[x][y]will be initiallynull.