我一直试图弄清楚我的问题已经存在了很长时间,但老实说我无法弄清楚如何解决这个错误,直到我解决了这个错误,我无法检查我的其余部分程序,看它是否有效。
我正在构建一个程序,允许您输入一个.txt文件,其中包含构成迷宫的字符(#= wall,。= path,S = start,G = goal),并使用递归计算机找到通过迷宫的路径并打印解决方案。
它一直在说我在第32行遇到NullPointerException错误。我尝试过不同地初始化数组,尝试在循环之外初始化它,但我真的很难过。我将不胜感激任何帮助!这是我的整个代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
// MUST BE 10X5 ARRAY
public class TheMaze {
public static void main(String[] args) throws IOException {
//input the text file. test.txt should be whatever your maze is
String inputDoc = "test.txt";
FileReader fr = new FileReader(inputDoc);
BufferedReader br = new BufferedReader(fr);
//if your maze has dimensions different than 10x5, change these x and y values accordingly
int x = 10;
int y = 5;
char [][] maze = new char [x][y];
int xPosition = 0;
int yPosition = 0;
//now... to get the string into the 2d char array.
for (int rows = 0; rows < maze.length; rows++) {
String thisLineString;
char thisLineArray[] = new char [x];
thisLineString = br.readLine();
thisLineArray = thisLineString.toCharArray();
for (int columns = 0; columns < maze[0].length; columns++) {
maze[rows][columns] = thisLineArray[columns];
}
}
//find starting position
for(int i = 0; i < maze.length; i++) {
for(int j = 0; j < maze[0].length; j++) {
if (maze[i][j] == 'S') {
xPosition = i;
yPosition = j;
}
}
}
// i dont think this actually does anything because we end up printing the solved maze withib the function. cant make
//it a void because that would mess up the recursion stuff going on
char randomArray [][] = FindPath(maze , xPosition, yPosition);
}
public static char [][] FindPath( char [][]maze, int xPosition, int yPosition) {
//first if to see if inbounds
if (0 <= xPosition && maze.length > xPosition && 0 <= yPosition && maze[0].length > yPosition) {
if (maze[xPosition][yPosition] == 'G') {
//solution found ,
// FIGURE OUT HOW TO print solved maze
//using for loops you kind of reverse engineer what you did putting it from a string to 2d char array
for (int one = 0; one < maze.length; one++) {
//resets the line after it's printed, first time around this does nothing
String lineOfMaze = "";
for (int two = 0; two < maze[0].length; two++) {
lineOfMaze = lineOfMaze + maze[one][two];
}
}
return maze;
} else if (maze[xPosition][yPosition] == '#') {
// wall
return maze;
} else if (maze[xPosition][yPosition] == '+') {
//already been here
return maze;
} else if (maze[xPosition][yPosition] == 'S' || maze[xPosition][yPosition] == '.'){
//when youre at the start, or on an open path
if (maze[xPosition][yPosition] == '.') {
//marks the spot as part of the path
maze[xPosition][yPosition] = '+';
}
if (FindPath(maze, xPosition, yPosition+1) != maze) {
//checks to the north
return maze;
} else if (FindPath(maze, xPosition+1, yPosition) != maze) {
//checks east
return maze;
} else if (FindPath(maze, xPosition, yPosition-1) != maze) {
//check south
return maze;
} else if (FindPath(maze, xPosition-1, yPosition) != maze) {
//checks west
return maze;
} else {
// no path found, start backtracking and remove this spot as being a place on the path, unless youre still at the start
//in which case it just doesn't do anything
if (maze[xPosition][yPosition] == '+') {
maze[xPosition][yPosition] = '.';
}
return maze;
}
}
}
return maze;
}
}
错误所在的行是thisLineArray = thisLineString.toCharArray();,并且它再次是NullPointerException错误。 任何帮助将不胜感激。