我正嘗試在java中使用控制檯類。與此代碼Java中的控制檯類讀取密碼的例外
import java.io.Console;
public class ConsoleClass {
public static void main(String[] args){
Console c=System.console();
char[] pw;
pw=c.readPassword("%s","pw :");
for(char ch:pw){
c.format("%c",ch);
}
c.format("\n");
MyUtility mu =new MyUtility();
while(true){
String name=c.readLine("%s", "input?: ");
c.format("output: %s \n",mu.doStuff(name));
}
}
}
class MyUtility{
String doStuff(String arg1){
return "result is " +arg1;
}
}
這裏我得到的NullPointerException當我試圖在NetBeans中運行,但試圖與出的NetBeans IDE.Why CMD運行時我沒有得到任何的異常?
你會得到一個堆棧跟蹤指示哪一行導致空指針異常? – 2011-03-11 16:09:17
如果沒有控制檯,System.console()將返回null。看到這裏:http://illegalargumentexception.blogspot.com/2010/09/java-systemconsole-ides-and-testing.html – McDowell 2011-03-11 16:10:23
@Michael McGowan我在pw = c.readPassword(「%s」,「pw: 「); – satheesh 2011-03-11 16:21:08