1
import java.io.Console; 

public class Talk {
    public static void main(String[] args) {
        Console c = System.console();
        String pw;
        System.out.print("password: ");
        pw = c.readLine();
        System.out.println("got " + pw);
    }
}

Why am I getting exception in above code?

password: Exception in thread "main" java.lang.NullPointerException
    at Talk.main(Talk.java:8)

Can anyone help me out?

4
  • 2
    Did you read the javadoc for System.console()? Commented Feb 12, 2014 at 20:03
  • 3
    Looks like c is null, which means you're not executing your Java application in a console like cmd but your IDE console. Commented Feb 12, 2014 at 20:03
  • The system console, if any, otherwise null. Commented Feb 12, 2014 at 20:06
  • Bug #122429 in Eclipse. Let me guess, you are running inside Eclipse. Commented Feb 12, 2014 at 20:07

5 Answers 5

2

Javadoc says why it returns null

console

public static Console console()

Returns the unique Console object associated with the current Java virtual machine, if any.

Returns: The system console, if any, otherwise null.

Since: 1.6

Intellij IDEA returns null too with System.console so the only thing you can do is to create two methods (one for read line, one for password since System.console have readPassword method) which helps you to avoid problems when switch to IDE to Production.

public static String readLine() throws IOException
{
    if (System.console() != null)
    {
        return System.console().readLine();
    }
    else
    {
        return new BufferedReader(new InputStreamReader(System.in)).readLine();
    }
}

public static char[] readPassword() throws IOException
{
    if (System.console() != null)
    {
        return System.console().readPassword();
    }
    else
    {
        return readLine().toCharArray();
    }
}

I chosed to keep the char[] way for readPassword but if you want you can convert it to string.

You can keep in memory the System.console reference to avoid double call to console() method which is syncronized (in my source code at least)

public static String readLine() throws IOException
{
    Console console = System.console();
    if (console != null)
    {
        return console.readLine();
    }
    else
    {
        return new BufferedReader(new InputStreamReader(System.in)).readLine();
    }
}

public static char[] readPassword() throws IOException
{
    Console console = System.console();
    if (console != null)
    {
        return console.readPassword();
    }
    else
    {
        return readLine().toCharArray();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

From System#console javadoc:

Returns the unique Console object associated with the current Java virtual machine, if any.

Returns

The system console, if any, otherwise `null`.

If you want to use System#console then you must execute your Java application since a console like Windows CMD or Linux console. If you happen to run this application since your IDE e.g. Eclipse, Netbeans, IntelliJ, etc, you will get null value since they're not real consoles.

If you happen to work with Eclipse, you can refer to this Q/A to make it work in Eclipse: java.io.Console support in Eclipse IDE

Comments

0

According to the API: If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

Also you can find some additional information System.console() returns null

Comments

0

The answer of Luiggi Mendoza explains your problem very well. That is, in IDEs the System.console() function returns NULL.

However, if you want to run this Java program without any exception being thrown, simply open a console or command prompt and type the following 2 commands and your program runs like shown below:

>cd path/to/class/file
>java -cp . Talk
password: 325
got 325

This assumes that you have the java command in your PATH. Also don't forget to change path/to/class/file to the actual path where your .class file (not .java file) is located on your system. I tried it on Linux and it works fine.

Comments

0

If you are running your app inside Eclipse, I'd suggest you use java.util.Scanner to read inputs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.