0

I am trying to get the input from the from the user (hidden) and trying to print that password, but the console is null and getting NullPointerException.

Is readPassword() supported in java? What is the substitute for this?

Console cons;
if((cons = System.console()) != null) {
    char[] password = null;

    try {   
        System.out.println("Enter the password :");
        password=cons.readPassword();    
        System.out.println("Your password is" + new String(password));
    } finally {
        if(password != null) {
            java.util.Arrays.fill(password,' ');
        }
    }
} else {
    throw new RuntimeException("can't get password...No console");
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • If `readPassword` didn't exist, you wouldn't get a `NullPointerException`. You wouldn't even be able to compile. – Federico klez Culloca Mar 26 '18 at 07:08
  • What line throws the NPE? – NielsNet Mar 26 '18 at 07:12
  • 1
    The problem is that eclipse doesn't support `System.console()`! For more info see: [System.console() returns null from Eclipse but fine with Command Prompt](https://stackoverflow.com/q/8969990/8097737) and [java.io.Console support in Eclipse IDE](https://stackoverflow.com/q/104254/8097737) –  Mar 26 '18 at 07:15
  • possible duplicate of https://stackoverflow.com/questions/4203646/system-console-returns-null – Absent Mar 26 '18 at 07:17
  • Possible duplicate of [System.console() returns null](https://stackoverflow.com/questions/4203646/system-console-returns-null) – sauerburger Mar 26 '18 at 07:21

1 Answers1

1

The problem is that there is an open bug in Eclipse that leads to System.console being NULL. Related question:

How to read password from console without using System.console()?

Still open bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429

Also see the duplicates of that bug.

kutschkem
  • 7,826
  • 3
  • 21
  • 56