Hey guys,

I'm creating a Maze class along with a client class where it allows the user to input a direction (U/L/R/D), and then the validity of the move will be returned as well as the new position of the maze. So far, I've got this, but when I try to run it, two nullPointExceptions are thrown and I've tried fixing them but can't seem to understand as to why they're being thrown..

Any help would be appreciated,
Ecliptical.

//Maze.Java
//This program creates a maze, and allows the user to attempt to solve it through the client class.
//
//

import javax.swing.JOptionPane;
import java.lang.*;

public class Maze
{
  int numRows, numCols, row, col;
  String charInputs;
  String[][]maze;
  final String p, f, l, r, d, u, move;
  
  public Maze()
  {
    row = 0;
    col = 0;
    charInputs = "";
    move = "";
    d = "D";
    l = "L";
    r = "R";
    u = "U";
    p = "P";
    f = "F";
    String[][]maze = { {p, p, p, f}, {f, f, p, f},{f, f, p, f},{f, f, p, p} };
  }
  
  public void solveMaze(String charInput)
  {
    numRows = maze.length;
    numCols = maze.length;
    {
      //Check to see if the location is in within the grid.
      if(row > numRows || col > numCols || row < 0 || col < 0)
      {
        JOptionPane.showMessageDialog(null, "Out of bounds! Choose a new direction");
      }
      
      //Check to see if the person has solved the maze.
      else if(row == numCols && col == numRows)
      {
        JOptionPane.showMessageDialog(null, "You have solved the maze!");
      }
      
      
      //Check user's input for the move, and make sure that the move is valid.
      else if(charInput.equals("R") && maze[col+1][row].equals("p"))
      {
        col += 1;
        JOptionPane.showMessageDialog(null, "This is a part of the path");
        solveMaze(charInput);
      }
      
      else if(charInput.equals("D") && maze[col][row+1].equals("p"))
      {
        row += 1;
        JOptionPane.showMessageDialog(null, "This is a part of the path");
        solveMaze(charInput);
      }
      
      
      else if(charInput == "L" && maze[col-1][row].equals("p"))
      {
        col -= 1;
        JOptionPane.showMessageDialog(null, "This is a part of the path");
        solveMaze(charInput);
      }
      
      
      else if(charInput.equals("U") && maze[col][row-1].equals("p"))
      {
        row -= 1;
        JOptionPane.showMessageDialog(null, "This is a part of the path");
        solveMaze(charInput);
      }
      
      //If none of the moves are valid, then the user must choose a new direction.
      else
      { 
        JOptionPane.showMessageDialog(null, "The location which you chose to move to is blocked. Please choose a new direction.");
        solveMaze(charInput);
      }
    }
  }
//This is the client class for the Maze.java program.
//

import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;

public class MazeDriver
{
  public static void main( String[] args )
  {
    Maze labyrinth = new Maze();
    String move = "";
    move = JOptionPane.showInputDialog(null, "Choose the direction (U/L/D/R) which you wish to move");
    labyrinth.solveMaze(move);
  }
}

Hey!

I took me a while but with a little help from my compiler I finally spotted your error.

int numRows, numCols, row, col;
  String charInputs;
  String[][]maze; // RIGHT HERE!!!!
  final String p, f, l, r, d, u, move;
  
  public Maze()
  {
    row = 0;
    col = 0;
    charInputs = "";
    move = "";
    d = "D";
    l = "L";
    r = "R";
    u = "U";
    p = "P";
    f = "F";
    String[][]maze = { {p, p, p, f}, {f, f, p, f},{f, f, p, f},{f, f, p, p} }; // AND HERE!!!
  }

The first error I found was In this block of code. It has to do with variable scope. This string array is recognized by the compiler from the line it is declared on until the end of the code block in which it was declared. The problem is that you have a nested code block where you re-declare the same string array and initialize it. That second declaration isn't accessible outside of the code block in which it was defined, which is inside the Maze constructor. So when you try to use the maze.length method, the compiler tells you that you are trying to access a null value… NullPointerException. To fix it you would have to change the declaration of the first String array to this

String[][] maze = new String[/*some number*/][/*some number*/];

This isn't where the troubles end however, I suspect that the Second declaration of maze creates an entirely different object. So when you try and access the elements in maze you are actually accessing the elements of the first maze object, which are null, so you continue to get a NullPointerException thrown.

That's all I've been able to understand so far, I might continue to look into it if I get the time.

Good luck with what I've told you so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.