1

I'm using jgrasp and I'm unsure of why I'm getting this error. Specifically:

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)

I've tried rewriting the scanner part, but I don't know what I'm doing. I'll include the line numbers with comments. Here is my code:

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;
   }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
boop
  • 55
  • 1
  • 3
  • 12

1 Answers1

1

The problem is in handleArguments() with this line (and other copies of it):

String mazefile = args[0];

This statement declares and assigns a value to a local variable, which is shadowing (hiding) the field of the same name.

From the Java Language Specification, section 14.4.3:

If a name declared as a local variable is already declared as a field name, then that outer declaration is shadowed (§6.3.1) throughout the scope of the local variable.

yet in the main() method, you pass the field mazefile to create the file:

readMazeFile(mazefile);

but the field mazefile is still unassigned - ie null.

If fix the problem, assign args[0] to the field:

mazefile = args[0];
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks for your reply. I made the edits, but now I am receiving this error message: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String Index out of ranged: 0 – boop Feb 08 '15 at 02:14
  • Use a debugger. Learn to use a debugger. Seriously dude, StackOverflow is NOT a "debug my code for free" service. @Bohemian - IMO you are encouraging the OP to be lazy. He is supposed to be learning to do this for himself. – Stephen C Feb 08 '15 at 02:27
  • @stephenc normally I would agree with you, but I spotted a coding blunder that was more than an oversight of not initialising a field, but of a shadowing issue. I accept that OP may have eventually figured it out (and teach a man to fish etc). I thought it could be valuable to others. – Bohemian Feb 08 '15 at 03:20
  • @user3780506 that is a different question: Ask a new question (don't ask a new question as a comment) – Bohemian Feb 08 '15 at 03:27
  • I see. Yes the problem is not as trivial as I first thought. – Stephen C Feb 08 '15 at 03:27