比较两个字符串时出现空指针异常



在比较两个字符串时,我不断得到一个空指针怀疑。我知道这两个字符串都不为空,所以我不确定发生了什么。

public void search() {
    while (!openList.isEmpty()) {
        currState = openList.removeFirst();

        if (currState.equals(goal)) { //this line produces NullPointerException
            solution = true;
            printSolution(currState);
            break;

目标是我从文件中读入的字符串。开放列表是一个链表。

字符串开头为: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);
    }
}

解决方案

试试这个,在加载 goalstart 的值之前删除对 addToOpenList(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也看到了这个:)

方法调用 currState.indexOf("0") 在第 115 行可能会产生 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();
    }
}

最新更新