Java 简单静态主程序中的NullPointerException

Java 简单静态主程序中的NullPointerException,java,eclipse,if-statement,java.util.scanner,Java,Eclipse,If Statement,Java.util.scanner,我不知道我做错了什么 Exception in thread "main" java.lang.NullPointerException at helloWorld.HelloWorld.main(HelloWorld.java:30) 请告诉我一个更好的方法。 这个错误似乎是我在使用扫描仪时出错了 第13行的“String feels1=scan2.nextLine();”。我想可能有一种更简单的方法来写这个,但我只是在测试 public static void main(Strin

我不知道我做错了什么

Exception in thread "main" java.lang.NullPointerException
    at helloWorld.HelloWorld.main(HelloWorld.java:30)
请告诉我一个更好的方法。 这个错误似乎是我在使用扫描仪时出错了 第13行的“String feels1=scan2.nextLine();”。我想可能有一种更简单的方法来写这个,但我只是在测试

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变量从不为非null。换句话说,您在何处为其赋值:

feels = "something"; // ????
在使用它之前给它一个字符串

更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念您应该仔细阅读异常的stacktrace,找到出错的代码行,即引发异常的代码行,然后仔细检查该行,找出哪个变量为null,然后回溯到代码中以了解原因。相信我,你会一次又一次地碰到这些

例如,您的错误消息告诉您查看第30行:
HelloWorld.java:30
,我敢打赌您在这一行尝试取消引用feels变量。看到这一点,您应该知道在使用之前检查您的代码,并将其分配给这个变量。这样做,你会立即发现你的错误


编辑

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

do {
   String feels = "something"
} while(feels.equalsIgnoreCase(foo));
但这里的问题是feels在do块内声明,并且仅在do块内可见(在形成块的花括号内),因此在while的布尔条件内不可见。您希望在do块之前声明feels,以便while条件可以使用它。例如

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

原创海报,在未来,请努力为您的问题发布一个信息丰富的标题。这一次我是为你做的,错误是告诉你是第30行出了故障,而不是你假设的第13行。请注意:
HelloWorld.java:30
。请为我们发布该行。
String=null然后执行循环,但不为该变量赋值,您使用的是
feels1
,而我知道错误的真正位置。还是不行。字符串feels1=“”;仍然会使最终产品出错。在我的情况下。控制台:用户,你今天过得怎么样?[好][坏][好]坏我很高兴你感觉很好@用户3739057:哦,见鬼,感觉1与感觉无关。这是两个名称完全不同的变量。来吧,我本来就是这个意思。我不知道如何链接它们,即使它们是同一个do while的一部分loop@user3739057:链接它们?见鬼,不,摆脱感觉。只使用feels和feels而已。它告诉我,在while循环的第二部分中,feels没有定义“feels无法解析”