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

how come i keep getting java.lang.NullPointerException whenever i use console.readLine in applications like eclipse?

I copied my whole code from the Treehouse workspace into eclipse and bluej and i keep getting java.lang.NullPointerException at the first console.readLine i know the code works because it works in treehouse.

2 Answers

Maybe check your imports? make sure you have import java.io.Console;

if you are trying to get input from the console you could also use the scanner class. it would look something like: make sure you import java.util.Scanner;

Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();

The problem is that the Console class does not work in most Java IDEs due to the way they run your program. This stackoverflow post provides a more detailed explanation as to why it does not work if you are curious. If you compiled your program in Eclipse and ran it in a console manually then it would work fine.

This means that you have to use alternative ways to print and get input when working in your IDE.

To print you can use System.out instead of console like this:

System.out.println("Text you want to be printed to console");

System.out also has a printf and print method just like the Console class, that works in the same way as the Console equivalents

And to get input you can use the Scanner class like this:

import java.util.Scanner; // Place this at the top of your code to import the Scanner

Scanner scanner = new Scanner(System.in); // Write this in your method once
String input = scanner.nextLine(); // Then use scanner.nextLine as a substitute for console.readLine

How come Treehouse teaches us console? Will they teach us the alternatives?

They likely teach the Console because it is a bit simpler than the alternatives. You have one object that is used for both input and output. And it also supports printing a prompt when input is asked for. For example this code:

console.readLine("Enter your name: ")

Would result in "Enter your name: " being printed to the console as it prompted for input. The nextLine method for the Scanner class does not support specifying a message in that way. With Scanner you have to manually print out a message before you prompt for input.

I have not gone through all of the Java courses on Treehouse so I don't know if they cover the alternatives in a later course, but I do know they have some courses that use an IDE (IntelliJ IDEA) so I assume they explain the alternative commands in those courses.