pacman4565
pacman4565

Reputation: 125

Null pointer exception when comparing two Strings

I keep getting a Null Pointer Esception when comparing two Strings. I know both of the Strings aren't null so I am not sure what is going on.

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


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

Goal is a String that I read in from a file. Openlist is a linked list.

the string start is: 120345678 and goal is: 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);

    }

}

Upvotes: 1

Views: 326

Answers (2)

MariuszS
MariuszS

Reputation: 31595

Solution

Try this, remove invocation of addToOpenList(start, null) before loading value of goal and start.

Old stuff

Here is null

addToOpenList(start, null);

String currState = openList.removeFirst();

currState == null

Additional information

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
}

Even my IntelliJ see this :)

Method invocation currState.indexOf("0") at line 115 may produce java.lang.NullPointerException

115:  a = currState.indexOf("0");

Upvotes: 2

Josh M
Josh M

Reputation: 11947

Perhaps you could try something like:

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

Upvotes: 0

Related Questions