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 Regular Expressions in Java

JT Keller
JT Keller
12,731 Points

What to do for the Java tutorials if you're working out of an IDE and are trying to use the Console but get an error?

I'm actually going to answer my own question so that this is logged in case anyone else runs into this issue.

3 Answers

JT Keller
JT Keller
12,731 Points

I've never used the Console class and was eager to try it out when I saw that Craig was using it in the regex course. However, I develop from within an IDE so the Console class will return a null exception if you're working in an IDE.

There are a couple of workarounds. You can just run the classes externally by setting the built classes in the bin directories on the JRE classpath or use a wrapper like one of the below.

Scanner userInput = new Scanner(System.in);
String line = userInput.nextLine();

//or

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();

Hope this helps :)

Thank you JT. I was loosing my mind.

Thank you for the help!

I was confused as to where I put those lines of code, but here's what worked for me in case anyone in the future needs more help:

Change ==> Console console = System.console(); To This ==> Scanner userInput = new Scanner*System.in);

Change ==> String zipCode = console.readLine("Enter your zipcode: "); To This ==> System.out.print("Enter your zipcode: "); String zipCode = userInput.nextLine();

I'm only halfway through the video, but this got my initial code to work.

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

JT Keller Thank you for that unrequested help :) Do you know by any chance why System.console() returns a null inside of IDE?

My class mentor told me because there is no console in the IDE ???

Thanks Laura and Andre, I'm using IDE as well and this was frustrating.