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?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2985842
  • 437
  • 9
  • 24

5 Answers5

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();
    }
}
Community
  • 1
  • 1
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
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

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
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

Community
  • 1
  • 1
Evgeny Kiselev
  • 216
  • 1
  • 8
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.

Benny
  • 607
  • 7
  • 20
0

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

Max Seo
  • 196
  • 6