NullPointerException 并将一个数组初始化为另一个 class

NullPointerException and initializing an array into another class

我正在尝试实施深度优先算法来解决像这样的迷宫,其中 S 是第一个位置,E 是 "finish line"

_SW______\n
_WWW_W_WW\n
_____W__E\n

但是我在不应该发生的情况下收到了 NullPointerException。 这是我 "import" 二维数组迷宫,迷宫首先从 .txt 文件读取到深度 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);

            }




        }
    }



}

这里是深度优先代码

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

这里是主要内容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());
    }
}
}

我遇到了这个错误

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) 

我想这个问题可能与在深度中首先启动迷宫阵列的问题有关class,但我不确定。

问题如下:由于您在 Main class 中将 reader 声明为静态变量,因此在执行主代码之前先执行其构造函数。在您的构造函数中,您正在访问 "Maze" 变量。但由于您之前没有调用过 readTextFile,因此它为空。

它唯一一次从 null 变为实际数组是在这一行中:

Maze=new char[numrows][numCols];

在此之前(尤其是在你的构造函数中),它是空的。

另外:尽量避免到处使用 static。