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 Java Basics Using your New Tools Errors

Karim Zibari
Karim Zibari
7,358 Points

I keep getting the NullPointerException when program reaches: String n=console.readLine("What's the name?");

I keep getting the NullPointerException when my Java program reaches the line: String n=console.readLine("What's the name?");

This is the complete program: import java.io.Console; public class Test{ public static void main(String[] args){ Console console=System.console(); String name="What's your name? "; System.out.println(name); String n=console.readLine("What's the name?"); } }

1 Answer

Gyorgy Andorka
Gyorgy Andorka
13,811 Points

This is probably because you're running your program in an IDE, which doesn't use the console. Your code would work in Workspaces, or if executed from the command line. As an alternative, you can use the Scanner class inside of your IDE. (Scanner scanner = new Scanner(System.in); note that you have to print a separate prompt message (if needed) before using e.g. the .nextLine() method for reading the input)