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

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();