Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Recursive Maze Help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursive Maze Help

    I am making a recursive maze that has to find the path. I have got it pretty well set up but I got the following errors when I ran it.
    Any Help is greatly appreciated!

    Maze Solver

    1. Maze 1
    2. Maze 2
    3. Maze 3
    4. Random Maze

    Enter Number: 1

    ############
    #...#......#
    ..#.#.####.#
    ###.#....#.#
    #....###.#..
    ####.#.#.#.#
    #..#.#.#.#.#
    ##.#.#.#.#.#
    #........#.#
    ######.###.#
    #......#...#
    ############Exception in thread "main" java.lang.NullPointerException
    at MazeSolver.<init>(MazeSolver.java:24)
    at MazeDriver.main(MazeDriver.java:106)

    public class MazeDriver {
     
    	public static void main(String args[])	
    	{
    		int input; //Number of maze
    		Maze m = null;
    		Scanner scanner = new Scanner(System.in);
     
    		char[][] m1 = {{'#','#','#','#','#','#','#','#','#','#','#','#'},
                    {'#','.','.','.','#','.','.','.','.','.','.','#'},
                    {'.','.','#','.','#','.','#','#','#','#','.','#'},
                    {'#','#','#','.','#','.','.','.','.','#','.','#'},
                    {'#','.','.','.','.','#','#','#','.','#','.','.'},
                    {'#','#','#','#','.','#','.','#','.','#','.','#'},
                    {'#','.','.','#','.','#','.','#','.','#','.','#'},
                    {'#','#','.','#','.','#','.','#','.','#','.','#'},
                    {'#','.','.','.','.','.','.','.','.','#','.','#'},
                    {'#','#','#','#','#','#','.','#','#','#','.','#'},
                    {'#','.','.','.','.','.','.','#','.','.','.','#'},
                    {'#','#','#','#','#','#','#','#','#','#','#','#'}};
     
     
    		char[][] m2 = {{'#','#','#','#','#','#','#','#','#','#','#','#'},
                    {'#','.','.','.','#','.','.','.','#','#','.','.'},
                    {'#','.','#','.','.','.','#','.','.','.','.','#'},
                    {'#','.','#','#','#','#','.','#','.','#','.','#'},
                    {'#','.','.','.','#','#','.','.','.','.','.','#'},
                    {'#','#','#','.','#','#','#','#','.','#','.','#'},
                    {'.','.','.','.','.','.','.','.','.','.','#','#'},
                    {'#','#','#','#','#','#','#','#','#','#','#','#'}};
     
    		char[][] m3 = {{'#','#','#','#','#','#','#','#','#'},
                    {'#','.','#','.','#','.','.','.','#'},
                    {'#','.','.','.','#','.','#','#','#'},
                    {'#','#','#','.','#','.','#','.','.'},
                    {'.','.','.','.','.','.','#','.','#'},
                    {'#','#','.','#','.','#','#','.','#'},
                    {'#','.','.','#','.','#','.','.','#'},
                    {'#','#','.','#','.','#','.','.','#'},
                    {'#','#','#','#','#','#','#','#','#'}};
     
     
    			do
    			{
     
    				System.out.println("Maze Solver");
    				System.out.println("\n1. Maze 1");
    				System.out.println("2. Maze 2");
    				System.out.println("3. Maze 3");
    				System.out.println("4. Random Maze");
     
     
    				System.out.print("\nEnter Number: ");
    				input = scanner.nextInt();
    				if(input < 1 || input > 4)
    				{
    					System.out.println("Invalid Number!");
    				}
    			}while(input < 1 || input > 4);
     
     
    			switch(input)
    			{
     
    				case 1:
    					m = new Maze(m1, 2, 4);
    				break;
     
    				case 2:
    					m = new Maze(m2, 6, 1);
    				break;
     
    				case 3:
    					m = new Maze(m3, 4 , 3);
     
    				break;
     
    				case 4:
    					m = new Maze();
    				break;
     
    			}	
     
    			System.out.print(m.toString());
    			MazeSolver ms = new MazeSolver(m, m.start, m.finish);
    			ms.solve(0, ms.s);
     
     
     
     
    	}
    }
    public class MazeSolver {
    	int s;
    	int f;
    	Maze solveMaze;
    	int x;
    	int y;
    	int size = solveMaze.Maze[0].length;
        boolean solved;
        boolean wall = false;
        int count=0;
     
     // Method Name		: MazeSolver
     	// Parameters		: Maze m, int start, int finish
     	// Partners     	: None
     	// Description		: Initializes Variables 
    	public MazeSolver(Maze m, int start, int finish)
        {
        	s=start;
        	f=finish;
        	solveMaze = m;
        }
     
    	 // Method Name		: Solve
     	// Parameters		: int x, int y
     	// Partners     	: None
     	// Description		: Solves the maze.
    	public void solve(int x, int y) 
    	{
            if (x==size && y==f) 
            {
                solved = true;
            }
            solveMaze.Maze[x][y] = 'x';
     
            if (solveMaze.Maze[x + 1][y] == '.') 
            {
                solve(x + 1, y);
                count++;
            }
            else if (solveMaze.Maze[x][y + 1] == '.') 
            {
                solve(x, y + 1);
                count++;
            }
            else if (solveMaze.Maze[x - 1][y] == '.') 
            {
                solve(x -1, y);
                count++;
            }
            else if (solveMaze.Maze[x][y-1] == '.') 
            {
                solve(x, y - 1);
                count++;
            }
            else 
            {
                wall = true;
            }
            if (wall) 
            {
                wall = false;
                solve(x, y);
            }
            if (solved)
            {
            	System.out.print("solved");
            	solveMaze.Maze.toString();
            }
     
     
     
     
     
     
    	}
     
    }
    public class Maze {
     
    	char Maze[][];
    	public int start;
    	public int finish;
    	public int width;
    	public int width2;
     
     
    	 // Method Name		: Maze
    	// Parameters		: char n, int s, int f
    	// Partners     	: None
    	// Description		: Initializes preset maze
     
    	public Maze(char[][] m, int s, int f)
    	{
    		Maze= m;
    		start=s;
    		finish=f;
    	}
    public String toString()
    	{
    	    String output="";
    	    for (int i=0; i<Maze.length; i++)
    	    {
    	    	for (int j =0; j<Maze[i].length; j++)
    	    	{
    	    		if(j==0)
    	    		{
    	    			output += "\n";
     
    	    		}
    	    		output += Maze[i][j];
    	    	}
    	    }
    		return output;
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Recursive Maze Help

    Exception in thread "main" java.lang.NullPointerException
    at MazeSolver.<init>(MazeSolver.java:24)
    Look at line 24 in MazeSolver and see what variable has a null value. Then backtrack in the code to see why that variable does not have a non-null value. If you can't tell which variable is null, add a println in front of line 24 that prints out the values of all the variables on line 24

Similar Threads

  1. Mouse Maze..Plz Help
    By TiT_4_tAt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2012, 07:33 AM
  2. BackTracking in a maze
    By skoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2012, 10:23 AM
  3. Maze Game
    By whatsbruin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 05:59 PM
  4. need help creating a maze program
    By helpzor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 03:12 PM
  5. Recursion Maze
    By sman1234 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2010, 11:40 AM