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