java.lang.NullPointerException的问题

时间:2014-03-23 12:51:16

标签: java nullpointerexception keystore keytool

我的程序有问题。我想访问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: ");

的对齐

我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

您的System对象不会返回有效的Console对象。并非所有人都允许这样做。

根据系统API:

  

返回与当前Java虚拟机关联的唯一Console对象,(如果有)   返回:
            系统控制台(如果有)否则为null 。   <emphasis mine>

答案 1 :(得分:1)

您可能正在尝试从IDE运行该应用程序 - 这就是System.console()返回null的原因。尝试从命令行运行应用程序。