'Kotlin/Java java.security.cert.Certificate.getType() null reference

I'm having issue with SSL connection using client certificate and key - java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.security.cert.Certificate.getType()' on a null object reference

tracking back to this line -

sslContext.init(kmf?.keyManagers, trustManagerFactory.trustManagers, null)

here is how i create kmf

val kmf : KeyManagerFactory? = if (privateKey != null) {
            val keystoreClient = KeyStore.getInstance(keyStoreType)
            keystoreClient.load(null, null);
            keystoreClient.setKeyEntry(
                "cn", privateKey, "Printec".toCharArray(), arrayOf(caServer, caClientPass, caClientCert)
            )

            KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).apply {
                this.init(keystoreClient, "Printec".toCharArray()) }
        } else {
            null
        }

here is full stack trace

2022-02-15 12:44:16.684 7014-7048/cz.printec.tms.app W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.security.cert.Certificate.getType()' on a null object reference
2022-02-15 12:44:16.684 7014-7048/cz.printec.tms.app W/System.err:     at java.security.KeyStore$PrivateKeyEntry.<init>(KeyStore.java:569)
2022-02-15 12:44:16.684 7014-7048/cz.printec.tms.app W/System.err:     at java.security.KeyStore$PrivateKeyEntry.<init>(KeyStore.java:526)
2022-02-15 12:44:16.684 7014-7048/cz.printec.tms.app W/System.err:     at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:485)
2022-02-15 12:44:16.684 7014-7048/cz.printec.tms.app W/System.err:     at java.security.KeyStore.getEntry(KeyStore.java:1560)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at com.android.org.conscrypt.KeyManagerImpl.<init>(KeyManagerImpl.java:71)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at com.android.org.conscrypt.KeyManagerFactoryImpl.engineGetKeyManagers(KeyManagerFactoryImpl.java:114)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at javax.net.ssl.KeyManagerFactory.getKeyManagers(KeyManagerFactory.java:305)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at cz.printec.tms.app.activities.TmsScript.useSSLConnection(TmsScript.kt:255)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at cz.printec.tms.app.activities.TmsScript.access$useSSLConnection(TmsScript.kt:30)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at cz.printec.tms.app.activities.TmsScript$tmsConnect$hurlStack$1.createConnection(TmsScript.kt:81)
2022-02-15 12:44:16.685 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.toolbox.HurlStack.openConnection(HurlStack.java:215)
2022-02-15 12:44:16.686 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:83)
2022-02-15 12:44:16.686 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
2022-02-15 12:44:16.686 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
2022-02-15 12:44:16.686 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
2022-02-15 12:44:16.686 7014-7048/cz.printec.tms.app W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

I really don't know what is the Certificate instance, I have printec RSA key in log, looks good. Please ask questions if you need me to add/explain or provide more context.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source