0

I am trying to implement the Depth first algorithm to solve a maze like this one, where S is the first position and E the "finish line"

_SW______\n
_WWW_W_WW\n
_____W__E\n

but i am getting the NullPointerException when it shouldnt happen. Here is where i "import" the 2d array Maze with the maze read from the .txt file into the Depth first class

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 {
                if (row.length() >= j) {
                    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);

            }




        }
    }



}

Here is the depth first 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 am getting this 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) 

I think the problem may be related to a problem when it comes to initiating the Maze array in the Depth first class but I am not sure.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

The problem is the following: Due to you declaring reader as a static variable in the Main class, its constructor is executed before your main code is executed. In your constructor you are accessing the "Maze" variable. But since you haven't called readTextFile before, it is null.

The only time it changes from null to the actual array is in this line:

Maze=new char[numrows][numCols];

before that (especially in your constructor), it is null.

Also: try to avoid using static all over the place.

peterulb
  • 2,869
  • 13
  • 20
  • yes that´s the solution to the problem, but if i do DepthFirst.searchPath(Maze, 0, 1, path); at the end of the readtextFile class shouldnt it work? because i am getting an ArrayIndexOutOfBoundsException: -1 –  Jun 10 '17 at 23:56
  • @MDordio Well then there is another flaw in your logic. Somehow x or y is decreased to a value below 0, which doesn't exist in an array. There are two positions where this can happen, x+dx where dx is -1 and x is 0 initially, or later on the same where y is decreased by one. I would recommend running the debugger step for step for one run in your IDE – peterulb Jun 11 '17 at 00:19
  • i will look into that, thank you –  Jun 11 '17 at 00:25