Exception in thread "main" java.lang.NullPointerException at javaapplication5.login.login(Main.java:20)
at javaapplication5.login.main(Main.java:34)
Java Result: 1
System.console() can return null is no console is available. For instance in the netbeans console.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky is right - System.console() is the only way to do this on the console. And Wouter is right too, the console is only available if you launched your application from a real console; IDEs don't have a real console.
Note that you will not get stars; instead nothing is printed at all.
akash shrimali
Ranch Hand
Joined: Jul 28, 2010
Posts: 45
posted
0
Console console = System.console();
if (console != null) {
System.out.println("Console is not available");
System.exit(1);
}
System.out.println("PASSWORD \n");
char[] passw;
passw= console.readPassword("[%s]", "Password:") ;
for(int i=0;i<=passw.length;i++)
{
System.out.print("*");
if(passw[i]==13)
{
break;
}
passw[i]='\0';
}
}