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);
}
}