1

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

    }

}
8
  • 2
    100% currState is null, why? I don't know from the code you've posted. Commented Nov 17, 2013 at 17:29
  • 2
    I know both of the Strings aren't null so I am not sure what is going on. I don't believe you. Commented Nov 17, 2013 at 17:29
  • currState IS null, probably because openList's first element is null. Definitely check currState ;) Commented Nov 17, 2013 at 17:32
  • Open lists first element shouldn't be null because it contains the string start? Commented Nov 17, 2013 at 17:53
  • @MarounMaroun I added the rest of my code. Maybe I was a bit rash when saying " I know it is not null". Commented Nov 17, 2013 at 18:07

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

6 Comments

Or, assuming goal isn't null (which presumably it isn't), goal.equals(currState). Or even Objects.equals(currState, goal)
I still don't understand why addToOpenList(start, null); makes currState null. The second argument of addToOpenList is the oldState which is null at the start. Why does that make start null as well?
And why does the while statement not loop?
This is another question :) Get focus on this question :)
@MariuszS I can't figure out why you think the while statement doesn't loop. I think the problem lies with the load methods because when I enter the Strings "start" and "goal" myself instead of using the load methods the program works.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.