下面我们有两个文件中的第一个代码,名为Game(继续向下滚动以查看第二个文件+问题)
public class Game
{
private static final int PLAYING=0;
private static final int RED_WINS=1;
private static final int BLACK_WINS=2;
private static final int DRAW=3;
private static final int RED=4;
private static final int BLACK=5;
private static final int EMPTY=6;
private static final int WALL=7;
private static int board[][];
public Game()
{
int board[][]={{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL}};
}
public void draw()
{
for(int r= 0; r< board.length; r++)
{
for(int c= 0; c<board[0].length; c++)
{
if(board[r][c]==WALL)
{
char Wall='|';
board[r][c]=Wall;
}
if(board[r][c]==EMPTY)
{
char Empty=' ';
board[r][c]=Empty;
}
if(board[r][c]==BLACK)
{
char Black='B';
board[r][c]=BLACK;
}
if(board[r][c]==RED)
{
char Red='R';
board[r][c]=RED;
}
System.out.print(board[r][c] + "");
}
System.out.print("\n");
}
}
}
以下是第二个文件MainFile
中的代码
import java.util.Scanner;
public class MainFile
{
public static void main(String[]args)
{
Game a=new Game();
a.draw();
}
}
当我按下编译时,输出显示进程已完成,一开始看起来一切正常,但是当我按下运行文件时,我得到以下运行错误,我能知道我做错了什么以及如何解决?
Exception in thread "main" java.lang.NullPointerException
at Game.draw(Game.java:28)
at MainFile.main(MainFile.java:7)