Java NullPointerException并将数组初始化到另一个类中
我试图实现深度优先算法来解决这样一个迷宫,其中S是第一个位置,E是“终点线” 但是我得到了NullPointerException当它不应该发生的时候。 在这里,我将2d数组迷宫与从.txt文件读取的迷宫“导入”到深度优先类中Java NullPointerException并将数组初始化到另一个类中,java,arrays,exception,depth-first-search,Java,Arrays,Exception,Depth First Search,我试图实现深度优先算法来解决这样一个迷宫,其中S是第一个位置,E是“终点线” 但是我得到了NullPointerException当它不应该发生的时候。 在这里,我将2d数组迷宫与从.txt文件读取的迷宫“导入”到深度优先类中 public class MazeReader extends JFrame { private static char[][] Maze; private static ArrayList<String> mazeBuffer; private stati
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);
}
}
}
}
我得到了这个错误
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)
线程“main”java.lang.ExceptionInInitializeError中的异常
位于java.lang.Class.forName0(本机方法)
位于java.lang.Class.forName(Class.java:264)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
原因:java.lang.NullPointerException
在DepthFirst.searchPath(DepthFirst.java:10)
在MazeReader(MazeReader.java:23)
我认为这个问题可能与在深度优先类中启动迷宫数组的问题有关,但我不确定。问题如下:由于您在主类中将reader声明为静态变量,因此在执行主代码之前会执行其构造函数。在构造函数中,您正在访问“Maze”变量。但因为您以前没有调用过readTextFile,所以它是空的 它从null更改为实际数组的唯一时间是在这一行:
Maze=new char[numrows][numCols];
在此之前(尤其是在构造函数中),它是空的
另外:尽量避免到处使用static。在调用readTextFile之前是否实例化MazeReader?所有代码都发布在这里,我没有做任何其他事情。没有main方法?我确信你在某处调用了=new MazeReader(),以及MazeReader.readtextfile是的,你是对的,我在这里也添加了主类代码。你声明了
Maze
,但它没有初始化是的,这是问题的解决方案,但是如果我先执行DepthFirst.searchPath(Maze,0,1,path);在readtextFile类的末尾,它不应该工作吗?因为我得到了一个ArrayIndexOutOfBoundsException:-1@MDordio那么,你的逻辑还有一个缺陷。不知何故,x或y被减少到低于0的值,而这在数组中是不存在的。这可能发生在两个位置,x+dx,其中dx最初为-1,x为0,或者稍后在同一位置,y减小1。我建议您在IDE中一步一步地运行调试器,我会对此进行研究,谢谢
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)
Maze=new char[numrows][numCols];