Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.Console.readPassword(String, Object[])" because the return value of "java.lang.System.console()" is null at hello.Lautturi.main(Lautturi.java:108)
/**
* @author lautturi.com
* Java example: get/hide password on console / replace password with asterisk in java console
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
char[] passwordArray = System.console().readPassword("Please enter the password:");
for (int i = 0; i < passwordArray.length; i++) {
System.out.print("*");
}
System.out.println();
String password = new String(passwordArray);
System.out.println(password);
}
}
There's no console if we use javaw.exe instead of java.exe
use java.exe
F:\java\hello\bin>"F:\Program Files\Java\jdk-19\bin\java.exe" hello.Lautturi Please enter the password: lautturi
instead of
F:\java\hello\bin>"F:\Program Files\Java\jdk-19\bin\java.exe" hello.Lautturi F:\java\hello\bin>