Reputation: 13
I'm getting the error "Exception in thread "main" java.lang.NullPointerException" when I try to read an array value. Here is the code that I think causes the error.
class Board
{
private static char[][] board;
public Board(int r, int c)
{
setRow(r);
setColumn(c);
char board[][] = new char[row][column];
}
public void getBoard()
{
for (int c = 1;c <= getColumn()-1 ;c++)
{
System.out.print("\t"+c);
}
System.out.print("\n");
for (int r = 1; r <= getRow()-1; r++)
{
System.out.print(r);
for (int c = 1; c <= getColumn(); c++)
{
System.out.print("\t" + board[r][c]); //I think board[r][c] is causing it.
}
System.out.println("");
}
return;
}
}
I can upload the whole file if need be.
Any help would be appreciated, this kept me up last night.
Upvotes: 1
Views: 74
Reputation: 19185
You are hiding member variable in the constructor
char board[][] = new char[row][column];
it should be
board= new char[row][column];
Upvotes: 1
Reputation: 382434
Replace
char board[][] = new char[row][column];
with
board = new char[row][column];
In the first statement, you're assigning a value to a local variable, not to the one of your instance.
Upvotes: 2