# Recursion Maze error

Page 1 of 1

## 3 Replies - 801 Views - Last Post: 23 October 2013 - 07:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332205&amp;s=a3d0910b1485e2fb908d1523911dc1b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 titanguitarman

Reputation: 0
• 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)
{
}
}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

• D.I.C Regular

Reputation: 84
• Posts: 365
• 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?

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,401
• Joined: 27-December 08

## Re: Recursion Maze error

Posted 23 October 2013 - 07:14 AM

Better yet- post your stack trace.

### #4 Peter O

• D.I.C Regular

Reputation: 128
• Posts: 300
• 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?