-1

I don't know what I did wrong.

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

Feel free to tell me a much better way of doing this. The error seems like to be something I did wrong with the scanner during "String feels1 = scan2.nextLine();" on line 13. I think there might be a way easier way of writing this but I'm just testing.

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.");

    }               

}

Edited:

  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.");

            }                              

    }

1 Answers1

1

Your feels variable is never non-null. In other words, where do you ever assign it a value:

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

Give it a String before using it.

More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

For example, your error message is telling you to look at line 30: HelloWorld.java:30, and I'll bet that on this line you try to de-reference the feels variable. On seeing this, you should know to inspect your code to where you think you assign something to this variable before use. Do this, and you'd immediately identify your error.


Edit

Your latest code essentially looks like this:

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

But the problem here is that feels is declared inside of the do block and is only visible within the do block (within the curly braces that form the block), and is thus invisible within the while's boolean condition. You want to declare feels before the do block so that the while condition can use it. For e.g.,

String feels = "";
do {
   feels = "something"
} while(feels.equalsIgnoreCase(foo));
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • While, I understood where the error was really. It still wouldn't work. String feels1 = ""; Will still make the end product wrong. In my situation. Console: User, how are you doing today? [Good] [Bad] [Ok] bad Im glad you're feeling good! – user3739057 Feb 17 '15 at 01:35
  • @user3739057: Aw hell, feels1 has nothing to do with feels. Those are **two different variables with completely different names**. Come on now. – Hovercraft Full Of Eels Feb 17 '15 at 01:36
  • That's what I meant originally. I didn't know how to link them even if they're part of the same do-while loop – user3739057 Feb 17 '15 at 01:37
  • @user3739057: Link them? Heck no. Get **rid of** feels1. Just use feels and feels only. – Hovercraft Full Of Eels Feb 17 '15 at 01:37
  • It's telling me that feels is not defined in the second part of the while loop "feels cannot be resolved" – user3739057 Feb 17 '15 at 01:39
  • @user3739057: no links please. Instead please [edit your original question](http://stackoverflow.com/posts/28553184/edit) (click link) and post the new code as new lines to the bottom. – Hovercraft Full Of Eels Feb 17 '15 at 01:51
  • Ok, I edited it in chat. It's telling me that "feels cannot be resolved" which is why I made it "feels1" – user3739057 Feb 17 '15 at 01:56
  • @user3739057: You've got a variable scope problem now. in your latest code, you're declaring feels **inside** of the `do {...}` block of code, meaning that it is only visible within that block of code. In your first bit of code, you declare the feels variable at the beginning of the main method, meaning that it is visible within the entire method. Do this again, declare the variable at the beginning of the main method, do not *re-declare it* anywhere, and use it throughout the method. – Hovercraft Full Of Eels Feb 17 '15 at 01:59
  • @user3739057: see edit for an explanation of what I mean. – Hovercraft Full Of Eels Feb 17 '15 at 02:08
  • I got it. But a new problem persists and I don't think I should start another question. Editing now.. Wait hold on sorry this is bad – user3739057 Feb 17 '15 at 02:16
  • The issue is in the whole code. I will continue to work on it. Thanks for the help. – user3739057 Feb 17 '15 at 02:22