NPException in ArrayList? Java

So I keep getting the NPException when running my spell checker. The exception occurs in my "edits" method. So here is the code of the method:

    public ArrayList<String> edits (String word){
    ArrayList<String> result = new ArrayList<String>();

    result.add(word.substring(1));
    result.add(word.substring(0, word.length()-1));
    for (char c = 'a'; c <= 'z'; c++){
        result.add(String.valueOf(c) + word);
    }
    for (char c = 'a'; c <= 'z'; c++){
        result.add(word + String.valueOf(c));
    }

    for (int i = 0; i < word.length()-1; i++){
        char[] c = word.toCharArray();
        char temp = c[i];
        c[i] = c[i+1];
        c[i+1] = temp;
        String swappedword = new String(c);
        result.add(swappedword);
    }
    return result;
}

The error occurs at the 4th line, "result.add(word.substring(1));". I have looked at the related answers, but it didn't help me solve the problem. Help please! Thank you all!

Jon Skeet
people
quotationmark

The error occurs at the 4th line, "result.add(word.substring(1));"

Then word must be null. result clearly isn't. You should look at what's calling this code, and work out why it's passing in a null reference. You might also want to add some validation:

public ArrayList<String> edits (String word) {
    if (word == null) {
        throw new NullPointerException();
    }
    ...
}

That way it's immediately clear from the stack trace what's wrong. That's assuming you don't want to accept a null reference, of course - if you do, then just change the code above to return whatever you want. (I'd personally require word to be non-null though.)

people

See more on this question at Stackoverflow