3 Replies - 281 Views - Last Post: 23 October 2013 - 07:17 AM Rate Topic: -----

#1 titanguitarman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-October 13

Recursion Maze error

Posted 22 October 2013 - 09:09 PM

I have tried to get my code to work and it isn't. I keep getting null exceptions.


public class MazeSolver {
	Maze solveMaze;
	int s;
	int f;
	int x;
	int y;
	int nosolve = 0;
	int size = solveMaze.Maze[0].length;
    int count=0;
    boolean solved;
    boolean wall = false;

	
	public MazeSolver(Maze m, int start, int finish)
    {
    	s=start;
    	f=finish;
    	solveMaze = m;
    }

	public void solver(int x, int y) 
	{
        if (x==size && y==f) 
        {
            solved = true;
        }
        solveMaze.Maze[x][y] = 'x';
        
        if (solveMaze.Maze[x + 1][y] == '.') //go right
        {
            solver(x + 1, y);
            count++;
        }
        else if (solveMaze.Maze[x][y + 1] == '.') //go up
        {
            solver(x, y + 1);
            count++;
        }
        else if (solveMaze.Maze[x - 1][y] == '.') //go left
        {
            solver(x -1, y);
            count++;
        }
        else if (solveMaze.Maze[x][y-1] == '.') //go down
        {
            solver(x, y - 1);
            count++;
        }
        else 
        {
            wall = true;
        }
        
        if (wall) 
        {
            wall = false;
            solver(x, y);
        }
        
        if (solved)
        {
        	System.out.print("solved");
        }
                
	}
	
}







 import java.util.Scanner;


	     public class MazeDriver {

	     	public static void main(String args[])	
	     	{
	     		int input; //Number of maze
	     		Maze m = null; // Used for maze selection
	     		Scanner scan = new Scanner(System.in);
	      		
	       	char[][] m1 = {{'#','#','#','#','#','#','#','#','#','#','#','#'},   
		                    {'#','.','.','.','#','.','.','.','.','.','.','#'},
		                    {'.','.','#','.','#','.','#','#','#','#','.','#'},
		                    {'#','#','#','.','#','.','.','.','.','#','.','#'},
		                    {'#','.','.','.','.','#','#','#','.','#','.','.'},
		                    {'#','#','#','#','.','#','.','#','.','#','.','#'},
		                    {'#','.','.','#','.','#','.','#','.','#','.','#'},
		                    {'#','#','.','#','.','#','.','#','.','#','.','#'},
		                    {'#','.','.','.','.','.','.','.','.','#','.','#'},
		                    {'#','#','#','#','#','#','.','#','#','#','.','#'},
		                    {'#','.','.','.','.','.','.','#','.','.','.','#'},
		                    {'#','#','#','#','#','#','#','#','#','#','#','#'}};


		     char[][] m2 = {{'#','#','#','#','#','#','#','#','#','#','#','#'},
		                    {'#','.','.','.','#','.','.','.','#','#','.','.'},
		                    {'#','.','#','.','.','.','#','.','.','.','.','#'},
		                    {'#','.','#','#','#','#','.','#','.','#','.','#'},
		                    {'#','.','.','.','#','#','.','.','.','.','.','#'},
		                    {'#','#','#','.','#','#','#','#','.','#','.','#'},
		                    {'.','.','.','.','.','.','.','.','.','.','#','#'},
		                    {'#','#','#','#','#','#','#','#','#','#','#','#'}};

		     char[][] m3 = {{'#','#','#','#','#','#','#','#','#'},
		                    {'#','.','#','.','#','.','.','.','#'},
		                    {'#','.','.','.','#','.','#','#','#'},
		                    {'#','#','#','.','#','.','#','.','.'},
		                    {'.','.','.','.','.','.','#','.','#'},
		                    {'#','#','.','#','.','#','#','.','#'},
		                    {'#','.','.','#','.','#','.','.','#'},
		                    {'#','#','.','#','.','#','.','.','#'},
		                    {'#','#','#','#','#','#','#','#','#'}};
	     		
		     		System.out.println("Maze Solver");
	     			do
	     			{
	     				System.out.println("\n");
	     				System.out.println("1. Maze 1");
	     				System.out.println("2. Maze 2");
	     				System.out.println("3. Maze 3");
	     				System.out.println("4. Random Maze");


	     				System.out.print("\nEnter Choice: ");
	     				input = scan.nextInt();
	     				if(input < 1 || input > 4)
	     				{
	     					System.out.println("Please choose selections displayed!");
	     				}
	     			}while(input < 1 || input > 4);
	     		
	     			
	     			switch(input)
	     			{
	     				
	     				case 1:
	     					m = new Maze(m1, 3, 5);
	     				break;
	     				
	     				case 2:
	     					m = new Maze(m2, 7, 2);
	     				break;
	     				
	     				case 3:
	     					m = new Maze(m3, 5, 4);
	     					
	     				break;
	     				
	     				case 4:
	     					m = new Maze();
	     				break;
	     				
	     			}	
	     			
	     			System.out.print(m.toString()); //Print out Maze
	     			MazeSolver ms = new MazeSolver(m, m.start, m.finish);//Sends to Maze Class
	     			ms.solver(0, ms.s);

	     	}
	     }









public class Maze 
{

	char Maze[][];
	public int start;
	public int finish;
	
	
	public Maze(char[][] m, int s, int f)
	{
		Maze = m;
		start = s;
		finish = f;
	}
	
	public Maze ()
	{
		createMaze();
	}
	
	
	private char[][] createMaze() 
	{

		return null;
	}
	
	
	public String toString()
	{
	    String mazeout="";
	    for (int i=0; i<Maze.length; i++)
	    {
	    	for (int j =0; j<Maze[i].length; j++)
	    	{
	    		if(j==0)
	    		{
	    			mazeout += "\n";
	    		}
	    		mazeout += Maze[i][j];
	    	}
	    }
		return mazeout;
	}

}







Is This A Good Question/Topic? 0
  • +

Replies To: Recursion Maze error

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: Recursion Maze error

Posted 23 October 2013 - 07:10 AM

What line is causing the exception? Have you tried debugging it line by line?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,597
  • Joined: 27-December 08

Re: Recursion Maze error

Posted 23 October 2013 - 07:14 AM

Better yet- post your stack trace.
Was This Post Helpful? 0
  • +
  • -

#4 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 125
  • Joined: 19-October 13

Re: Recursion Maze error

Posted 23 October 2013 - 07:17 AM

Will not line 8 fail because solveMaze is still null?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1