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 trialKarim Zibari
7,358 PointsI 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
gyorgyandorka
13,811 PointsThis 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)