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

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 "},
        };
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please post your question again but put your code in code tags, and print the text of the error message you are getting.

This article has been dead for over six months. Start a new discussion instead.