NPE在尝试生成证书时

时间:2017-11-12 19:40:02

标签: java android x509certificate

尝试在Android上生成X509证书时出现以下错误

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.security.PublicKey java.security.cert.X509Certificate.getPublicKey()'

以下是我用来实现此目的的代码:

            AssetFileDescriptor assetFileDescriptor = this.getAssets().openFd("cert.cer");
        FileDescriptor securityCertificate = assetFileDescriptor.getFileDescriptor();

        FileInputStream fin = new FileInputStream(securityCertificate);
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        X509Certificate certificate = (X509Certificate) cf.generateCertificate(fin);

错误似乎发生在 cf.generateCertificate(fin)

1 个答案:

答案 0 :(得分:0)

通过使用InputStream而不是FileInputStream来解决这个问题,下面的代码对我来说很好:

mainApp.service('MyService', function () {
    var partsList = [
        {name: "selected_computer_type", value: ''},
        {name: "selected_computer_size", value: ''},
        {name: "selected_computer_color", value: ''}
    ];

    //iterates through partslist, finds object that matches and sets the new value
    this.setValue = function (name, value) {
        for (var i = 0; i < partsList.length; i++)
            if (partsList[i].name == name) partsList[i].value = value;
    };

    this.getValueByName = function (name) {
        for (var i = 0; i < partsList.length; i++)
            if (partsList[i].name == name)
                return partsList[i].value;
        return "";
    };
});