3

Here is my code:

System.out.println("Enter Username: ");
String unm=System.console().readLine();

System.out.println("Enter Password: ");
char[] pwd=System.console().readPassword();

System.out.println("Welcome: " + "" + " Your password is " + new String(pwd));

Why am I getting this error?

Enter Username:

Exception in thread "main" java.lang.NullPointerException at Cons.main(Cons.java:13)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Keval Pithva
  • 600
  • 2
  • 5
  • 21
  • The :13 means line 13, in the future it helps if you tell us which line this is :) – user1445967 Jan 01 '16 at 10:39
  • 2
    look at here http://stackoverflow.com/questions/26078106/system-console-gives-nullpointerexception-in-netbeans – Zia Jan 01 '16 at 10:45

5 Answers5

4

From the Javadoc:

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

If there is no console associated to the JVM, the pointed line is the call of a method on a null object, hence the exception.

How do you launch your application?

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. 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.

If you want to read the username from the standard input, you could use this code:

try {
    System.out.print("Enter Username: ");
    InputStreamReader streamReader = new InputStreamReader(System.in);
    BufferedReader bufferedReader = new BufferedReader(streamReader);
    String username = bufferedReader.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • 1
    The only answer here which actually addresses the question (_"Why I am getting this error?"_). – skomisa Feb 21 '19 at 09:02
3

That is because System.console()is returning null. The official documentation states (bold is mine to emphasize):

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.

You can see it here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
malaguna
  • 4,183
  • 1
  • 17
  • 33
0

You should use a BufferedReader or a Scanner class to read inputs from the console.

BufferedReader:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadConsoleSystem {
  public static void main(String[] args) {

    System.out.println("Enter something here: ");

    try {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String s = bufferRead.readLine();

        System.out.println(s);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
  }
}

Scanner:

import java.util.Scanner;

public class ReadConsoleScanner {

  public static void main(String[] args) {

    System.out.println("Enter something here: ");

    String sWhatever;

    Scanner scanIn = new Scanner(System.in);
    sWhatever = scanIn.nextLine();

    scanIn.close();
    System.out.println(sWhatever);
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hemang
  • 390
  • 3
  • 20
0
String unm = System.console().readLine();

You should do a null check at system.console first. On platforms where there isn't any console associate, this application will die with a null pointer exception.

Do a null check on whether any console is available. If not, read with the scanner:

    String userName;
    char[] password;
    Scanner scanner = null;
    Console console = System.console();
    if(console == null){
        scanner = new Scanner(System.in);
        System.out.println("Enter username: ");
        userName = scanner.next();
        // Read the password as well
    }
    else{
        System.out.println("Enter username: ");
        userName = console.readLine();
        // Read the password as well
    }

Note:

  • One advantage with the console, specially while reading the password, is that it won't get displayed on the screen. With the scanner, your password will be displayed on the screen.
  • The above code snippet is for demonstration purposes.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Somnath Musib
  • 3,548
  • 3
  • 34
  • 47
0

If you need to input only the username, the answer provided by System.console() gives a NullPointerException in NetBeans would have been sufficient.

However, you also need to input the password securely. For this, the code you provided is perfectly fine. You just have to compile the project and run it in a Console or Terminal, instead of your IDE.

E.g., if you are using Windows and NetBeans, build your project by clicking on menu item RunBuild Project. This will create a .jar file with the same name as that of your project.

Now open a cmd window, navigate to the path where your .jar file is located and then execute your project by typing java -jar YourProjectName.jar command (replace YourProjectName with the name of your project).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
neutrino2039
  • 126
  • 10