Java Java Basics Getting Started with Java Receiving Input

Exception in thread "main" java.lang.NullPointerException at coding.treehouse.main(treehouse.java:11)

package coding;

import java.io.Console;

public class treehouse {

public static void main(String[] args) {

    Console console = System.console();

    String firstName = console.readLine("What's your name?  ");

    console.printf("Hello my name is %s\n", firstName);

}

}

1 Answer

Brendan Whiting
MOD
Brendan Whiting
Front End Web Development Treehouse Moderator 84,108 Points

I'm not 100% sure, but I've seen people have this error before using this System.console() in their own IDE. I think the best solution is to do these exercises in the Treehouse workspace where this console is built into the environment.

Outside of Treehouse, there is more than one way of receiving input from the command line. I prefer the Scanner class.