[英]Null pointer exception when comparing two Strings
比较两个字符串时,我一直收到Null Pointer Esception。 我知道两个字符串都不为null,所以我不确定发生了什么。
public void search() {
while (!openList.isEmpty()) {
currState = openList.removeFirst();
if (currState.equals(goal)) { //this line produces NullPointerException
solution = true;
printSolution(currState);
break;
目标是我从文件中读取的字符串。 Openlist是一个链接列表。
字符串开始为: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);
}
}
试试这个,取出的调用addToOpenList(start, null)
的负载值之前goal
和start
。
这是空的
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");
也许您可以尝试类似的方法:
public void search(){
currState = openList.removeFirst();
while(currState != null){
if(currState.equals(goal)){
solution = true;
printSolution(currState);
break;
}
currState = openList.removeFirst();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.