在比较两个字符串时,我一直得到一个Null Pointer Esception。我知道两个字符串都不为空,所以我不确定发生了什么。
public void search() {
while (!openList.isEmpty()) {
currState = openList.removeFirst();
if (currState.equals(goal)) { //this line produces NullPointerException
solution = true;
printSolution(currState);
break;
目标是我从文件中读取的字符串。 Openlist是一个链表。
字符串start是:120345678 目标是:012345678
public class BFS {
public String start;
public String goal;
public String startFinal;
LinkedList<String> openList;
Map<String, Integer> levelDepth;
Map<String, String> stateHistory;
int nodes = 0;
int limit = 100;
int unique = -1;
int newValue;
int a;
public String currState;
boolean solution = false;
public BFS() {
openList = new LinkedList<String>();
levelDepth = new HashMap<String, Integer>();
stateHistory = new HashMap<String, String>();
this.start = start;
this.goal = goal;
addToOpenList(start, null);// add root
}
public void loadStartState(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
StringBuilder currentLine = new StringBuilder();
while (line != null) {
currentLine.delete(0, currentLine.capacity());
currentLine.append(line);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
sb.append("\n");
line = reader.readLine();
}
start = sb.toString();
System.out.println(start);
} finally {
reader.close();
}
}
public void loadGoalState(String filename)throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
StringBuilder currentLine = new StringBuilder();
while (line != null) {
currentLine.delete(0, currentLine.capacity());
currentLine.append(line);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
sb.append("\n");
line = reader.readLine();
}
goal = sb.toString();
System.out.println(goal);
} finally {
reader.close();
}
}
public void search() {
while (!openList.isEmpty()) {
currState = openList.removeFirst();
if (currState != null && currState.equals(goal)) {
solution = true;
printSolution(currState);
break;
} else {
a = currState.indexOf("0");
// left
while (a != 0 && a != 3 && a != 6) {
String nextState = currState.substring(0, a - 1) + "0"
+ currState.charAt(a - 1)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// up
while (a != 0 && a != 1 && a != 2) {
String nextState = currState.substring(0, a - 3) + "0"
+ currState.substring(a - 2, a)
+ currState.charAt(a - 3)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// right
while (a != 2 && a != 5 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.charAt(a + 1) + "0"
+ currState.substring(a + 2)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// down
while (a != 6 && a != 7 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.substring(a + 3, a + 4)
+ currState.substring(a + 1, a + 3) + "0"
+ currState.substring(a + 4);
addToOpenList(nextState, currState);
nodes++;
break;
}
}
}
}
private void addToOpenList(String newState, String oldState) {
if (!levelDepth.containsKey(newState)) {
newValue = oldState == null ? 0 : levelDepth.get(oldState) + 1;
unique++;
levelDepth.put(newState, newValue);
openList.add(newState);
stateHistory.put(newState, oldState);
}
}
答案 0 :(得分:2)
尝试此操作,在加载addToOpenList(start, null)
和goal
之前删除start
的调用。
这是null
addToOpenList(start, null);
String currState = openList.removeFirst();
currState == null
其他信息
public BFS() {
this.start = start; // Variable 'start' is assigned to itself
this.goal = goal; // Variable 'goal' is assigned to itself
addToOpenList(start, null); // start is null
}
即使我的IntelliJ
看到了这一点:)
第115行的方法调用currState.indexOf(“0”)可能会产生 显示java.lang.NullPointerException
115: a = currState.indexOf("0");
答案 1 :(得分:0)
也许你可以尝试类似的东西:
public void search(){
currState = openList.removeFirst();
while(currState != null){
if(currState.equals(goal)){
solution = true;
printSolution(currState);
break;
}
currState = openList.removeFirst();
}
}