0

I'm trying to figure out some basics in Java. The code I've typed below compiles just fine, but not running. Everytime the code runs, it says NullPointerException error occurs.

class testOnStrings {
    public static void main (String args []){
        String input, output;

        // isWordPalindrome
        input = "forever eating cheese";
        boolean boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");

        input = "fall leaves when leaves fall";
        boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");

        input = null;
        boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");
    }

    static String reverseWords(String input) {
        // My code
        String words[] = input.split(" ");
        String reverseWord = "";
        for (int pointer = words.length - 1; pointer >= 0; pointer--)
            reverseWord += words [pointer] + " ";
        return reverseWord;
    }

    static boolean isWordPalindrome(String input) {
        // My code
        String reverseWordInput = reverseWords(input);
        boolean isPalindrome = input.equals(reverseWordInput);
        return isPalindrome;
    }
}

There is an error when running the code.

Exception in thread "main" 
java.lang.NullPointerException
    at idle.reverseWords(idle.java:21)
    at idle.isWordPalindrome(idle.java:30)
    at idle.main(idle.java:15)

Please help because the compilation is successful but code is not running

2 Answers2

2

You have assigned input as null hence the issue

input = null;
boolOutput = isWordPalindrome(input);

assign empty string value to input in case you don't want anything

input = "";
boolOutput = isWordPalindrome(input);

Reference :

Difference between null and empty string

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

I would recommend handling special/corner cases in the method rather than expecting callers to behave with empty or null string.

You can enhance isWordPalindrome method to handle null, zero or single character so that any kind of input can be handled.

FROM

static boolean isWordPalindrome(String input) {
    // My code
    String reverseWordInput = reverseWords(input);
    boolean isPalindrome = input.equals(reverseWordInput);
    return isPalindrome;
}

TO

static boolean isWordPalindrome(String input) {
    // My code
    if (input == null || input.length() == 0 || input.length() == 1) return true;
    String reverseWordInput = reverseWords(input);
    boolean isPalindrome = input.equals(reverseWordInput);
    return isPalindrome;
}

Above enhancement will also avoid calling reverseWords for null, zero or single character.

On same note, I would also enhance reverseWords also.

FROM

static String reverseWords(String input) {
    // My code
    String words[] = input.split(" ");
    String reverseWord = "";
    for (int pointer = words.length - 1; pointer >= 0; pointer--)
        reverseWord += words [pointer] + " ";
    return reverseWord;
}

TO

static String reverseWords(String input) {
    // My code
    if (input == null || input.length() == 0 || input.length() == 1) return input;
    String words[] = input.split(" ");
    String reverseWord = "";
    for (int pointer = words.length - 1; pointer >= 0; pointer--)
        reverseWord += words [pointer] + " ";
    return reverseWord;
}
JRG
  • 4,037
  • 3
  • 23
  • 34