2015-02-08 20 views
0

我使用jgrasp,我不確定爲什麼我得到這個錯誤。具體來說:異常在線程「主」java.lang.NullPointerException但我分配給字段

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) 

我試過重寫掃描儀部分,但我不知道我在做什麼。我會在行號中添加註釋。這裏是我的代碼:

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; 
    } 
} 
+0

'檔案文件=新的文件(mazefile);''當然是mazefile'有效? – Shahar 2015-02-08 00:41:00

回答

1

的問題是在handleArguments()這一行(和它的其他副本):

​​

此語句聲明和值分配給局部變量,這是遮蔽(隱藏)同名的字段

從Java語言規範,section 14.4.3

如果聲明爲一個局部變量的名稱已經被聲明爲字段名,則該外聲明在整個範圍陰影(第6.3.1節)局部變量。

但在main()方法,你通過mazefile創建文件:

readMazeFile(mazefile); 

但是場mazefile仍然未分配 - 即null

如果解決這個問題,分配args[0]

mazefile = args[0]; 
+0

感謝您的回覆。我做了編輯,但現在我收到此錯誤消息:線程「main」中的異常java.lang.StringIndexOutOfBoundsException:String索引超出範圍:0 – boop 2015-02-08 02:14:29

+0

使用調試器。學習使用調試器。認真花花公子,StackOverflow不是「免費調試我的代碼」服務。 @波希米亞 - 國際海事組織你鼓勵OP是懶惰的。他應該學習爲自己做這件事。 – 2015-02-08 02:27:35

+0

@stephenc通常我會同意你的看法,但是我發現了一個編碼錯誤,而不是初始化一個字段,而是一個陰影問題。我接受OP可能最終發現它(並教導一個人釣魚等)。我認爲這可能對其他人有價值。 – Bohemian 2015-02-08 03:20:36

相關問題