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 Receiving Input

Jared Cannon
Jared Cannon
422 Points

This doesn't run in Netbeans when I use this. I get a Exception in thread "main" java.lang.NullPointerException error.

Why are you using console.printf(); instead of system.out.print(); ? This will confuse a lot of people because it doesn't seem to be working. I don't know what I am doing wrong and why the console.printf(); is getting errors.

2 Answers

Clovis Mugaruka
Clovis Mugaruka
2,772 Points

It doesn't work because while using System.console() in any IDE (NetBeans, Eclipse, etc), the value of console is null hence the NullPointerException error. It will work fine in a terminal or a windows command prompt (I'm tempted to say because they return a Console Object) while IDEs implementation do not return a console object, but a null.

A simple test, you can run in your IDE to check whether console is null:

import java.io.Console;

/**
 * Demo Class
 */
public class Demo {
    /**
     * Starts program execution.
     *
     * @param args - command line arguments
     */
    public static void main(String[] args){
        Console console = System.console();

        if (console == null){
            System.out.println("CONSOLE is NULL!");
        }else{
            // do what you intended to do:-)
        }
    }
}

Here is a post on overcoming such issue in an IDE.

Hi Jared!

Both the console and system.out methods are valid ways to print to the console so it is really just preference as to which one you use. Now, let's see if we can figure out why you are experiencing errors with your code.

Do you mind pasting it here for me to take a look at?

-Luke