简单静态主程序中的 NullPointerException

NullPointerException in simple static main program

我不知道我做错了什么。

Exception in thread "main" java.lang.NullPointerException
    at helloWorld.HelloWorld.main(HelloWorld.java:30)

欢迎告诉我更好的方法。 这个错误似乎是我在扫描仪上做错了什么 "String feels1 = scan2.nextLine();" 第 13 行。我认为可能有更简单的方法来编写此代码,但我只是在测试。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Hello");
    System.out.println("Who am I speaking with?");
    String name = scan.nextLine();
    System.out.println("Hello " + name + "!"); 
    String feels = null;

    do{
        System.out.println(name + ", how are you doing today?");
        System.out.println("[Good] [Bad] [Ok]");
        Scanner scan2 = new Scanner(System.in);
        String feels1 = scan2.nextLine();
        if (!feels1.equalsIgnoreCase("good") || !feels1.equalsIgnoreCase("bad")
                || !feels1.equalsIgnoreCase("ok")){
            break;
        } else {
            System.out.println("I don't understand you.");
        }   
    } while ( !feels.equalsIgnoreCase("good") ||!feels.equalsIgnoreCase("bad")||
            !feels.equalsIgnoreCase("ok") );


            // The error lies here.
            if ( !feels.equalsIgnoreCase("good") ){
                System.out.println("Im glad you're feeling good!");

            }else if (!feels.equalsIgnoreCase("bad")){
                System.out.println("I hope you feel better!");

            }else{
                System.out.println("I'm sure you'll feel better soon enough.");

    }               

}

已编辑:

  public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                System.out.println("Hello");
                System.out.println("Who am I speaking with?");
                String name = scan.nextLine();
                System.out.println("Hello " + name + "!");

            do{
                    Scanner scan2 = new Scanner(System.in);
                    String feels = scan2.nextLine();
                    System.out.println(name + ", how are you doing today?");
                    System.out.println("[Good] [Bad] [Ok]");
                    if (!feels.equalsIgnoreCase("good") || !feels.equalsIgnoreCase("bad")
                                    || !feels.equalsIgnoreCase("ok")){
                            break;
                    } else {
                            System.out.println("I don't understand you.");
                    }      
            } while ( !feels.equalsIgnoreCase("good") ||!feels.equalsIgnoreCase("bad")||
                            !feels.equalsIgnoreCase("ok") );
                            if ( !feels.equalsIgnoreCase("good") ){
                                    System.out.println("Im glad you're feeling good!");

                            }else if (!feels.equalsIgnoreCase("bad")){
                                    System.out.println("I hope you feel better!");

                            }else{
                                    System.out.println("I'm sure you'll feel better soon enough.");

            }                              

    }

您的 feels 变量永远不会非空。换句话说,你在哪里给它赋值:

feels = "something"; // ????

在使用之前给它一个字符串。

更重要的是,您需要了解如何调试 NPE (NullPointerException) 的一般概念。 你应该批判性地阅读异常的堆栈跟踪,找到出错的代码行,抛出异常的行,然后仔细检查那行,找出哪个变量为空,然后跟踪回到你的代码看看为什么。相信我,你会 运行 一次又一次地进入这些。

例如,您的错误消息告诉您查看第 30 行:HelloWorld.java:30,我敢打赌您在这一行尝试取消引用 feels 变量。看到这里,您应该知道要检查您的代码,认为 在使用前为该变量赋值。这样做,您会立即发现您的错误。


编辑

您的最新代码基本上如下所示:

do {
   String feels = "something"
} while(feels.equalsIgnoreCase(foo));

但这里的问题是 feels 是在 do 块内部声明的,并且仅在 do 块内可见(在构成该块的花括号内),因此在 while 的布尔条件中是不可见的。你想声明 feels before do 块,以便 while 条件可以使用它。例如,

String feels = "";
do {
   feels = "something"
} while(feels.equalsIgnoreCase(foo));