通过方法时发生NullPointerException

时间:2018-08-31 17:05:03

标签: java runtime-error

我正在尝试弄清楚Java的一些基础知识。我在下面键入的代码可以正常编译,但无法运行。 每次代码运行时,都说发生NullPointerException错误。

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

运行代码时出错。

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)

请帮助,因为编译成功,但是代码未运行

2 个答案:

答案 0 :(得分:2)

您已将input分配为null,因此出现了问题

input = null;
boolOutput = isWordPalindrome(input);

分配空字符串值以供输入,以防万一

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

参考:

Difference between null and empty string

答案 1 :(得分:0)

我建议在方法中处理特殊情况/拐角情况,而不是期望调用方使用空字符串或null字符串。

您可以增强isWordPalindrome方法来处理nullzerosingle字符,以便可以处理任何类型的输入。

FROM

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

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

以上增强功能还将避免为reverseWordsnullzero字符调用single

同样,我也将增强reverseWords

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

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