I have to use constructors in my assignment so i decided to create two mazes (using arrays) and save them in two separate classes (maze1 and maze2). Then i called the array and save the reference to another array named mazeChoice (Line 23) . After, i try to print out the maze by calling the output method (line 24). However when i does this a nullpointerException error pops up.

Any Ideas? Thanks in advance

Here is the error

Exception in thread "main" java.lang.NullPointerException
at MazeAssignment.output(MazeAssignment.java:42)
at MazeAssignment.main(MazeAssignment.java:24)

import java.io.*;
public class MazeAssignment {
	public final String FINISH = "X ";
	public final String GO = ". ";
	public int MAXCOL = 12;
	public int MAXROW = 8;
	public boolean success = false;
	public static void main (String args[]) throws IOException {
		Maze1 mazeOne = new Maze1();
		String mazeChoice [][]=new String[8][12];
		Maze2 mazeTwo = new Maze2();
		MazeAssignment SA = new MazeAssignment();
		int x, y, userInput;
		System.out.println("Maze One");
		mazeChoice = mazeOne.structure;
		System.out.println();
		System.out.println("Maze Two");
		mazeChoice = mazeTwo.structure;
		System.out.println();
		System.out.println("Press 1 to solve Maze One, or press 2 to solve Maze 2");
		userInput = SA.userInput();
		if(userInput == 1){
			mazeChoice = mazeOne.structure;
			SA.output(mazeChoice);
		}
		else{
			mazeChoice = mazeTwo.structure;
			SA.output(mazeChoice);
		}
		System.out.println("Enter X Coordinate(Points 2 - 6)");
		x = SA.userInput();
		System.out.println("Enter Y Coordinate(Points 2 - 10)");
		y = SA.userInput();
		SA.output(mazeChoice);
		mazeChoice = SA.solveMaze(x, y, mazeChoice);
		mazeChoice = SA.cover(mazeChoice);
		System.out.println();
		SA.output(mazeChoice);
	}
	public void output(String mazeChoice[][]){
		for (int count1 = 0; count1 < 8; count1++){
			for (int count2 = 0; count2 < 12; count2++){
				System.out.print(mazeChoice[count1][count2]);
			}System.out.println();
		}
	}
	public int userInput () throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		return Integer.parseInt(br.readLine());
	}

class Maze1 {
	String structure[][];
	Maze1(){
		String structure [][] = {
				{" ", "1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "11 "},
				{"1 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B "},
				{"2 ", "B ", ". ", ". ", ". ", "B ", ". ", "B ", ". ", ". ", ". ", "B "},
				{"3 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", "B ", "B "},
				{"4 ", "B ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", "B "},
				{"5 ", "B ", ". ", "B ", "B ", "B ", "B ", ". ", "B ", "B ", ". ", "B "},
				{"6 ", "B ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", "B ", ". ", "B "},
				{"7 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "X ", "B ", "B ", "B "},
		};
	}
}
class Maze2{
	String structure[][];
	Maze2(){
		String structure [][] = {
				{" ", "1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "11 "},
				{"1 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B "},
				{"2 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", ". ", ". ", ". ", "B "},
				{"3 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", "B ", "B "},
				{"4 ", "B ", ". ", ". ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", "B "},
				{"5 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", "B ", "B ", ". ", "B "},
				{"6 ", "B ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", "B ", "X ", "B "},
				{"7 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B "},
		};
	}
}

If you post your code in code tags, it will appear with line numbers. That helps in determining where the problem occurs.

The way you are trying to initialize your arrays in Maze1 and Maze2 won't work. Try this instead:

class Maze2{
   String structure[][] = {
         {" ", "1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "11 "},
         {"1 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B "},
         {"2 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", ". ", ". ", ". ", "B "},
         {"3 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", "B ", "B "},
         {"4 ", "B ", ". ", ". ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", "B "},
         {"5 ", "B ", ". ", "B ", ". ", "B ", ". ", "B ", "B ", "B ", ". ", "B "},
         {"6 ", "B ", ". ", "B ", ". ", ". ", ". ", ". ", ". ", "B ", "X ", "B "},
         {"7 ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B ", "B "},
   };
}

I'm showing you the code for Maze2, but you have to change Maze1 as well.

class Maze1 {
String structure[][];
Maze1(){
String structure [][] = { /*you are creating another variable with the same name here*/

You have declared structure twice

The compiler will associate the array elements to the second declaration of structure which has a local scope.

Here is another way out ....
You can also write it like this :

class Maze1 {
	String structure[][];
	Maze1(){
		this.structure = { .............

Do the same for the other class as well.

vasu d, what you have suggested will not work. You can only use constants to initialize an array in the same statement where it is declared. Your approach will work if you use loops to initialize the array, but not with constants.

Kramerd, Thanks for pointing out the mistake ..... I had forgotten about that "special" type of array declaration.

However, it can be rectified by making the small modification below. Just add "new String[][]" and you have a nice one again.

The following code can also be used.

class Maze1 {
      String structure[][];
      Maze1(){
      this.structure = new String[][]{ .............
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.