Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个字符串时出现空指针异常_Java_Nullpointerexception_Equals - Fatal编程技术网

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

Java 比较两个字符串时出现空指针异常,java,nullpointerexception,equals,Java,Nullpointerexception,Equals,在比较两个字符串时,我总是得到一个空指针选项。我知道这两个字符串都不是空的,所以我不确定发生了什么 public void search() { while (!openList.isEmpty()) { currState = openList.removeFirst(); if (currState.equals(goal)) { //this line produces NullPointerException solut

在比较两个字符串时,我总是得到一个空指针选项。我知道这两个字符串都不是空的,所以我不确定发生了什么

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);

    }

}
公共类BFS{
公共字符串开始;
公共目标;
公共字符串开始结束;
LinkedList开放列表;
地图水平深度;
地图状态历史;
int节点=0;
整数极限=100;
int unique=-1;
int新值;
INTA;
公共字符串状态;
布尔解=假;
公共BFS(){
openList=newlinkedlist();
levelDepth=新的HashMap();
stateHistory=新HashMap();
this.start=start;
这个。目标=目标;
addToOpenList(开始,null);//添加根
}
公共void loadStartState(字符串文件名)引发IOException{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
试一试{
StringBuilder sb=新的StringBuilder();
字符串行=reader.readLine();
StringBuilder currentLine=新的StringBuilder();
while(行!=null){
currentLine.delete(0,currentLine.capacity());
currentLine.append(行);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
某人附加(“\n”);
line=reader.readLine();
}
start=sb.toString();
系统输出打印项次(开始);
}最后{
reader.close();
}
}
public void loadGoalState(字符串文件名)引发IOException{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
试一试{
StringBuilder sb=新的StringBuilder();
字符串行=reader.readLine();
StringBuilder currentLine=新的StringBuilder();
while(行!=null){
currentLine.delete(0,currentLine.capacity());
currentLine.append(行);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
某人附加(“\n”);
line=reader.readLine();
}
goal=sb.toString();
System.out.println(目标);
}最后{
reader.close();
}
}
公开无效搜索(){
而(!openList.isEmpty()){
currState=openList.removeFirst();
如果(currState!=null&&currState.equals(goal)){
解=真;
打印解决方案(currState);
打破
}否则{
a=当前状态的索引(“0”);
//左
而(a!=0&&a!=3&&a!=6){
字符串nextState=currState.substring(0,a-1)+“0”
+currState.charAt(a-1)
+当前状态子串(a+1);
addToOpenList(下一个州、当前州);
节点++;
打破
}
//向上
而(a!=0&&a!=1&&a!=2){
字符串nextState=currState.substring(0,a-3)+“0”
+当前状态子字符串(a-2,a)
+currState.charAt(a-3)
+当前状态子串(a+1);
addToOpenList(下一个州、当前州);
节点++;
打破
}
//对
而(a!=2&&a!=5&&a!=8){
字符串nextState=currState.substring(0,a)
+当前状态字符(a+1)+“0”
+当前状态子字符串(a+2)
+当前状态子串(a+1);
addToOpenList(下一个州、当前州);
节点++;
打破
}
//向下
而(a!=6&&a!=7&&a!=8){
字符串nextState=currState.substring(0,a)
+当前状态子字符串(a+3,a+4)
+当前状态。子字符串(a+1,a+3)+“0”
+当前状态子串(a+4);
addToOpenList(下一个州、当前州);
节点++;
打破
}
}
}
}
私有void addToOpenList(字符串newState、字符串oldState){
如果(!levelDepth.containsKey(newState)){
newValue=oldState==null?0:levelDepth.get(oldState)+1;
唯一++;
levelDepth.put(newState,newValue);
添加(newState);
stateHistory.put(newState、oldState);
}
}
解决方案 尝试此操作,在加载
goal
start
的值之前,删除对
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
也看到了这一点:)

第115行的方法调用currState.indexOf(“0”)可能会产生 java.lang.NullPointerException


或许您可以尝试以下方式:

public void search(){
    currState = openList.removeFirst();
    while(currState != null){
        if(currState.equals(goal)){
            solution = true;
            printSolution(currState);
            break;
        }
        currState = openList.removeFirst();
    }
}

100%
currState
null
,为什么?我从你发布的代码中不知道。我知道这两个字符串都不是null,所以我不确定发生了什么。我不相信你。
currState
null
,可能是因为
openList
的第一个元素是
null
。一定要检查
currState
;)Open list第一个元素不应该为null,因为它包含字符串start?@MarounMaroun我添加了其余的代码。也许我说“我知道它不是null”时有点鲁莽。或者,假设
goal
不是null(可能不是),
goal.equals(currState)
,甚至
Objects.equals(currState,goal)
我仍然不明白为什么addToOpenList(start,null);使currState为null。addToOpenList的第二个参数是oldState,它在开始时为null。为什么它也使start为null?为什么while语句不循环?这是另一个问题:)关注这个问题:)@MariuszS我不明白为什么你要这样做
public void search(){
    currState = openList.removeFirst();
    while(currState != null){
        if(currState.equals(goal)){
            solution = true;
            printSolution(currState);
            break;
        }
        currState = openList.removeFirst();
    }
}