Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Exception in thread "main" java.lang.NullPointerException

Here's my code:

package com.company;

import java.io.Console;

public class Main {

    public static void main(String[] args) {
        //Get console
        Console console = System.console();

        String response;
        boolean no;
        do {
            response = console.readLine("Do you understand do while loops?  ");
            no = (response.equalsIgnoreCase("no"));
            if (no) {
                console.printf("Come back when you understand them!");
            }
        }
        while (no);

    }
}

And the response it gives:

Exception in thread "main" java.lang.NullPointerException at com.company.Main.main(Main.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

2 Answers

Hi Zino!

In your boolean 'no' you are trying to store a string but that is not allowed as the variable is of type boolean. Because boolean variables can only be set to true or false your program is freaking out and throwing an error at you!

I hope this was of some help!

-Luke

That makes sense. Thanks, Luke!

What's weird is that I changed the input/output whatyacallit to "scanner" instead of console, and without changing the boolean like you said, the program works as expected:

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        //Get scanner
        Scanner scanner = new Scanner(System.in);

        String response;
        boolean no;
        do {
            System.out.println("Do you understand do while loops?  ");
            response = scanner.nextLine();
            no = (response.equalsIgnoreCase("no"));
            if (no) {
                System.out.println("Come back when you understand them!");
            }
        }
        while (no);

    }
}

I see from the last line of the error stack trace you're using IntelliJ IDEA. That's fine but there are know issues with using Console for input on it so you need to use Scanner with it like you posted.

The assignment of no is just fine because equalsIgnoreCase returns a boolean. If it wasn't it would have thrown a compiler error instead of a runtime error. The error was cause by the string coming from the Console being Null.