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 Java Basics Getting Started with Java Strings and Variables

Null pointer exception

Hi,

I am trying to run the same code in eclipse. I am getting the following error:

Exception in thread "main" java.lang.NullPointerException at <package_name>.Introductions.main(<line number>)

I had to use System.out.println() instead.

Thanks, Sharad

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Sharad - It would be hard to see what the problem is without seeing your code. Can you post it here?

package notDefault;

import java.io.Console;

public class Introductions {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Console console = System.console();     
        console.printf("Hello world");
    }

}

3 Answers

anil rahman
anil rahman
7,786 Points

Use scanner for reading user input in ide's

Scanner input = new Scanner(System.in); //Scanner lets you read input

System.out.print("Enter a word\n"); //prompt them to type word
String word = input.nextLine(); //store the word they type in variable word

System.out.printf("Your word was: %s",word);

Thanks a lot, that solves it.

The way I see it: It's sort of a work around, it works nonetheless. But, I really want to know how to debug the problem. What's the exact reason of the problem. Is it the version of Java? or something to do with the tool/environment setup? I think it's Java 8 on eclipse Mars 2.

Like you said earlier, the console.printf() function has 2 arguments, and the tutorial video leaves out the second argument, which works great in treehouse workspace, but does not in my system's eclipse. I think there is a difference in language version and setup environment/tool.

Thanks a lot though.

Seth Kroger
Seth Kroger
56,413 Points

All have issues with java.io.Console with their built-in terminals. IDE's. They try to extend the I/O classes to intercept the terminal I/O but Console if declares as final, i.e., not extendable. System.out works virtually the same as Console for output but for input I suggest using BufferedReader because it has the same readLine() method:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("Make a guess: ");
String guess = bufferedReader.readLine();

Note that printf() takes one or more arguments. The first argument is the format string, then enough to fill in the % placeholders in the format string. If the string has no % placeholders, it doesn't need a second arguemnt.

anil rahman
anil rahman
7,786 Points

it's because you are using printf without giving it a variable.

I would just scrap using the console in an ide like eclipse and go with the usual way which is

System.out.print("Hello World");

Or if you had variable then you can use printf also

String name = "World";
System.out.printf("Hello %s", name);

Thanks, but I am already using System.out.<printfunc>. How do I readLine without console object in further lessons?