“线程中的异常”;“主要”;java.lang.NullPointerException,但我正在分配给字段

“线程中的异常”;“主要”;java.lang.NullPointerException,但我正在分配给字段,java,nullpointerexception,Java,Nullpointerexception,我正在使用jgrasp,我不确定为什么会出现这个错误。具体而言: Exception in thread "main" java.lang.NullPointerException at java.io.File.<init>(File.java:277) at MazeSolver.readMazeFile(MazeSolver.java:87) at MazeSolver.main(MazeSolver.java:15) 线程“main”java.lang.NullPointe

我正在使用jgrasp,我不确定为什么会出现这个错误。具体而言:

Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at MazeSolver.readMazeFile(MazeSolver.java:87)
at MazeSolver.main(MazeSolver.java:15)
线程“main”java.lang.NullPointerException中的异常 在java.io.File.(File.java:277) 在MazeSolver.readMazeFile(MazeSolver.java:87) 在MazeSolver.main(MazeSolver.java:15) 我试着重写扫描仪的部分,但我不知道我在做什么。我会在评论中包括行号。这是我的密码:

public class MazeSolver {

   // The name of the file describing the maze
   static String mazefile;
   static int width;
   static int height;
   public static void main(String[] args) throws FileNotFoundException {
      if (handleArguments(args)) {

         readMazeFile(mazefile);  //line 15
         DrawMaze.draw();

         if (solveMaze())
            System.out.println("Solved!");
         else
            System.out.println("Maze has no solution.");
      }
      else {
         System.out.println("The arguments are invalid.");
      }
   }

   // Handle the input arguments
   static boolean handleArguments(String[] args) {
      if (args.length > 4 || args.length < 1) {
         System.out.println("There are too many or too few command line arguments");
         return false;
      }
      if (args.length == 1) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         return true;
      }
      if (args.length == 2) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         if (cellsize < 10) {
            return false;
         }
         return true;
      }
      if (args.length == 3) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         if (borderwidth < 5) {
            return false;
         }
         return true;
      }
      if (args.length == 4) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         int sleeptime = Integer.parseInt(args[3]);
         if (sleeptime < 0 || sleeptime > 10000) {
            return false;
         }
         return true;
      }   
      return false;
   }

   // Read the file describing the maze.
   static char[][] readMazeFile(String mazefile) throws FileNotFoundException {

      Scanner scanner = new Scanner(new File(mazefile)); //line 87
      height = scanner.nextInt();
      width = scanner.nextInt();
      int arrayHeight = 2  * height + 1;
      int arrayWidth = 2 * width + 1;
      char[][] mazeArrays = new char[arrayHeight][arrayWidth];
      while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         System.out.println(line);
         for (int row = 0; row < arrayHeight; row++) {
            for (int col = 0; col < arrayWidth; col++) {
               mazeArrays[row][col] = line.charAt(col);
            }
         }

      }
      return mazeArrays;
   }

   // Solve the maze.      
   static boolean solveMaze() {
      return true;
   }
}
公共类MazeSolver{
//描述迷宫的文件的名称
静态串迷宫;
静态整数宽度;
静态内高度;
公共静态void main(字符串[]args)引发FileNotFoundException{
if(手动学习单元(参数)){
readMazeFile(mazefile);//第15行
DrawMaze.draw();
if(solveMaze())
System.out.println(“已解决!”);
其他的
System.out.println(“迷宫没有解决方案”);
}
否则{
System.out.println(“参数无效”);
}
}
//处理输入参数
静态布尔handleArguments(字符串[]args){
如果(args.length>4 | | args.length<1){
System.out.println(“命令行参数太多或太少”);
返回false;
}
如果(args.length==1){
字符串mazefile=args[0];
文件文件=新文件(mazefile);
如果(!file.canRead()){
返回false;
}
返回true;
}
如果(args.length==2){
字符串mazefile=args[0];
文件文件=新文件(mazefile);
如果(!file.canRead()){
返回false;
}
int cellsize=Integer.parseInt(args[1]);
如果(单元大小<10){
返回false;
}
返回true;
}
如果(args.length==3){
字符串mazefile=args[0];
文件文件=新文件(mazefile);
如果(!file.canRead()){
返回false;
}
int cellsize=Integer.parseInt(args[1]);
int borderwidth=Integer.parseInt(args[2]);
如果(边框宽度<5){
返回false;
}
返回true;
}
如果(args.length==4){
字符串mazefile=args[0];
文件文件=新文件(mazefile);
如果(!file.canRead()){
返回false;
}
int cellsize=Integer.parseInt(args[1]);
int borderwidth=Integer.parseInt(args[2]);
int sleeptime=Integer.parseInt(args[3]);
如果(睡眠时间<0 | |睡眠时间>10000){
返回false;
}
返回true;
}   
返回false;
}
//阅读描述迷宫的文件。
静态字符[][]readMazeFile(字符串mazefile)引发FileNotFoundException{
Scanner Scanner=new Scanner(新文件(mazefile));//第87行
高度=scanner.nextInt();
宽度=scanner.nextInt();
内部阵列高度=2*高度+1;
int数组宽度=2*width+1;
char[]mazarrays=新字符[arraywhite][arrayWidth];
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
系统输出打印项次(行);
for(int行=0;行<数组高度;行++){
for(int col=0;col
此行(及其其他副本)的问题在于
handleArguments()

此语句声明一个局部变量并为其赋值,该局部变量隐藏同名字段

根据Java语言规范:

如果声明为局部变量的名称已声明为字段名,则外部声明将在局部变量的整个范围内隐藏(§6.3.1)

然而,在
main()
方法中,传递字段
mazefile
来创建文件:

readMazeFile(mazefile);
但是字段
mazefile
仍然未分配-即
null

如果解决了此问题,请将
args[0]
分配给字段:


File File=新文件(mazefile)确定
mazefile
有效吗?谢谢您的回复。我进行了编辑,但现在收到以下错误消息:线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:0使用调试器。学习使用调试器。说真的,伙计,StackOverflow不是“免费调试我的代码”服务@波希米亚人-在我看来,你是在鼓励OP懒惰。“他应该自己学习做这件事。”斯蒂芬斯通常我会同意你的看法,但我发现了一个编码错误,它不仅仅是没有初始化字段的疏忽,而是一个阴影问题。我承认OP最终可能已经找到了答案(并教人钓鱼等)。我认为它对其他人可能很有价值。@user3780506这是一个不同的问题:问一个新问题(不要问一个新问题作为评论),我明白了。是的,这个问题并不像我最初想的那么简单。
readMazeFile(mazefile);
mazefile = args[0];