当我尝试读取数组值时,我收到错误“线程中的异常”主“java.lang.NullPointerException”。这是我认为导致错误的代码。
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;
}
}
如果需要,我可以上传整个文件。
任何帮助都会受到赞赏,这让我昨晚感到高兴。
答案 0 :(得分:2)
替换
char board[][] = new char[row][column];
带
board = new char[row][column];
在第一个语句中,您要为局部变量赋值,而不是为您的实例赋值。
答案 1 :(得分:1)
您正在构造函数
中隐藏成员变量char board[][] = new char[row][column];
应该是
board= new char[row][column];