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

时间:2017-06-10 23:03:39

标签: java arrays exception depth-first-search

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

_SW______\n
_WWW_W_WW\n
_____W__E\n

但是当它不应该发生时我得到NullPointerException。 这里是我“导入”2d数组迷宫的地方,从.txt文件中读取的迷宫进入深度第一类

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

这是主要的课程

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) 

我认为问题可能与在深度头等舱中启动迷宫数组时的问题有关,但我不确定。

1 个答案:

答案 0 :(得分:0)

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

它从null更改为实际数组的唯一时间是在这一行:

Maze=new char[numrows][numCols];

之前(特别是在你的构造函数中),它是null。

另外:尽量避免在整个地方使用静电。