How to start an array in another class - java

1

I'm trying to implement the depth first algorithm to solve a labyrinth, like the one below, where the starting position is S and the output is E

_SW______\n
_WWW_W_WW\n
_____W__E\n

In the MazeReader class I read the .txt file for the Maze array which should then have its contents imported into the Depth class first

public class MazeReader extends JFrame  {
private static char[][] Maze;
private static ArrayList<String> mazeBuffer;
private static int startrow,startcol,finishcol,finishrow;
private final List<Integer> path = new ArrayList<Integer>();
private int pathIndex;

public MazeReader() {
    setTitle("Maze");
    setSize(640,480);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DepthFirst.searchPath(Maze, 0, 1, path);
    pathIndex = path.size() - 2;
}



public static void readTextFile(String filename) throws MazeFileWrongChar, MazeFileNumCols {
    startrow=startcol=finishcol=finishrow=-1;
    mazeBuffer=new ArrayList<String>();
    int numCols=0;
    try {
        Scanner file=new Scanner(new File(filename));
        while(file.hasNext()) {

            String nextLine=file.nextLine().replace("\n", "");
            mazeBuffer.add(nextLine);
            if(nextLine.length()>numCols) {
                numCols=nextLine.length();
            }



        }
    }catch(Exception e) {
        System.out.println(filename + "there is a problem");
    }
    int numrows=mazeBuffer.size();
    System.out.println("number of rows: "+ numrows +  "\nnumber of columns: " + numCols);
    Maze=new char[numrows][numCols];
    for(int i=0;i<numrows;i++) {
        String row = mazeBuffer.get(i);
        for (int j = 0; j < numCols; j++) {

            try {
                    Maze[i][j] = row.charAt(j);

            } catch (Exception e) {
                throw new MazeFileNumCols(j);


            }


            if(Maze[i][j]!=('S') && Maze[i][j]!=('W') && Maze[i][j]!=('_') && Maze[i][j]!=('E') && Maze[i][j]!=(' ') ) {
                throw new MazeFileWrongChar(i,j);

            }




        }
    }



}

Below the depth first class code

public class DepthFirst {
public static boolean searchPath(char[][] maze,int x,int y, List<Integer> path) {
    if(maze[y][x]=='E') {
        path.add(x);
        path.add(y);
        return true;
    }
    if(maze[y][x]=='_') {

        int dx=-1;
        int dy=0;
        if(searchPath(maze,x+dx,y+dy,path)) {
            path.add(x);
            path.add(y);
            return true;
        }

        dx = 1;
        dy = 0;
        if (searchPath(maze, x + dx, y + dy, path)) {
            path.add(x);
            path.add(y);
            return true;
        }

        dx = 0;
        dy = -1;
        if (searchPath(maze, x + dx, y + dy, path)) {
            path.add(x);
            path.add(y);
            return true;
        }

        dx = 0;
        dy = 1;
        if (searchPath(maze, x + dx, y + dy, path)) {
            path.add(x);
            path.add(y);
            return true;
        }
    }
    return false;
    }
    }

Here is the main class

public class Main {
static MazeReader reader = new MazeReader();

public static void main(String[] args) throws MazeFileWrongChar,MazeFileNumCols {
    try {
        MazeReader.readTextFile("maze.txt");
        reader.printMaze();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MazeReader view = new MazeReader();
                view.setVisible(true);
            }
        });

    } catch (MazeFileWrongChar e ) {
        System.out.println(e.getMessage());
    } catch(MazeFileNumCols e) {
        System.out.println(e.getMessage());
    }
}
}

I'm getting the following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.NullPointerException
    at DepthFirst.searchPath(DepthFirst.java:10)
    at MazeReader.<init>(MazeReader.java:23) 

But I'm having the NullPointerException error because the Maze array is not supposed to be initialized correctly, how can I do it?

    
asked by anonymous 12.06.2017 / 17:18

0 answers