我的程序有问题。我想访问keystore,用户给出pass和path。我的代码:
public class cipher_player {
public static void main(String[] args) throws Exception{
KeyStore klucz = KeyStore.getInstance("JCEKS");
Console konsola = System.console();
char passwordArray[] = konsola.readPassword("Password: ");
java.io.FileInputStream plik_keystore = null;
try{
Scanner scanner = new Scanner(System.in);
System.out.print("Path to keystore: ");
String pathArray = konsola.readLine();
System.out.print(pathArray);
plik_keystore = new java.io.FileInputStream(pathArray);
klucz.load(plik_keystore, passwordArray);
if(plik_keystore != null){
plik_keystore.close();
}
scanner.close();
}
catch(FileNotFoundException e)
{
System.out.println("Keystore not found");
System.exit(0);
}
}
}
当我运行它时显示此错误
Exception in thread "main" java.lang.NullPointerException
at crypto3.cipher_player.main(cipher_player.java:47)
与char passwordArray[] = konsola.readPassword("Password: ");
我不知道如何解决这个问题。
答案 0 :(得分:1)
您的System对象不会返回有效的Console对象。并非所有人都允许这样做。
根据系统API:
返回与当前Java虚拟机关联的唯一Console对象,(如果有) 返回:
系统控制台(如果有)否则为null 。<emphasis mine>
答案 1 :(得分:1)
您可能正在尝试从IDE运行该应用程序 - 这就是System.console()
返回null的原因。尝试从命令行运行应用程序。