gpt4 book ai didi

java - 尝试生成证书时出现 NPE

转载 作者:行者123 更新时间:2023-11-30 00:16:09 26 4
gpt4 key购买 nike

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

java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.PublicKey java.security.cert.X509Certificate.getPublicKey()' on a null object reference

下面是我用来实现这个的代码:

            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)

最佳答案

设法通过使用 InputStream 而不是 FileInputStream 来解决这个问题,下面的代码对我来说工作正常:

        InputStream inputStream = assetManager.open("cert.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
X509Certificate certificate = (X509Certificate) cf.generateCertificate(inputStream);

关于java - 尝试生成证书时出现 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com