I use AES encryption in the mobile app, but on rare occasions I get these errors in the Application onCreate class. Method "aesEncrypt" is often called elsewhere in the program, but does not cause an error. It seems that in some cases the internal variables don't have time to initialize, which causes the error to occur. According to Crashlytics reports, the problem occurs on Android 10, 11 and 12.
Are there anyone who has encountered this problem and what can be done?
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at java.security.Provider$Service.supportsKeyFormat(Provider.java:1850)
at java.security.Provider$Service.supportsParameter(Provider.java:1779)
at javax.crypto.Cipher.tryCombinations(Cipher.java:2889)
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2796)
at javax.crypto.Cipher.chooseProvider(Cipher.java:773)
at javax.crypto.Cipher.init(Cipher.java:1288)
at javax.crypto.Cipher.init(Cipher.java:1223)
at my.app.name.EncKt.aesEncrypt(EncKt.java:123)
my.app.name.App.onCreate (App.java:115)
Code:
fun aesEncrypt(cipher: String): String {
try {
val secretKeySpec = SecretKeySpec("abcdef1234567890".toByteArray(), "AES")
val instance = Cipher.getInstance("AES/CBC/PKCS5Padding")
instance.init(1, secretKeySpec, IvParameterSpec("0987654321fedcba".toByteArray()))
return Base64.encodeToString(instance.doFinal(cipher.toByteArray()), Base64.NO_WRAP)
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
return ""
}
}
UPD: My question was closed and related to the general question about NullPointerException. That's just great, thanks, I know what NPE is! Dear moderators, before you close the question next time, pay attention to where the error occurred. These are internal components of the operating system. This is a specific question about a specific component and a possible error in the OS during application initialization.