Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Gi Devs
Gi Devs
12,171 Points

console.readline in IntelliJ

I'm using a code Ive been working on in this project rather than the karaoke one (on the basis that using a code I've made myself will be better for my learning) when ever a console.readLine (for example :)

desiredPlayers = console.readLine("How many players are there? (maximum of 9 players): ");

is hit I get a null pointer exception and the code exits.

however this code works perfectly on the workspace what do I need to change to make it work in intelliJ

1 Answer

There are two ways:

First solution (used in the Treehouse-Courses I've seen where IntelliJ was used):

BufferedReader buffRd = new BufferedReader(new InputStreamReader(System.in));
String someText= buffRd.readLine();

Second solution (found with google... but didn't tried it yet):

Scanner scanner = new Scanner(System.in);
String someText = scanner.nextLine();