Skip to content

Commit 735efce

Browse files
authored
Merge pull request #643 from Kommunicate-io/Fix_NullPointerException_Crash
Added a null check in the code to add precaution over crash.
2 parents 3a5b985 + c71e87c commit 735efce

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,26 @@ public static boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTa
5656

5757
public static boolean isDatabaseEncrypted(Context context, String dbName) {
5858
String appId = MobiComKitClientService.getApplicationKey(AppContextService.getContext(context));
59+
60+
// 💡 DEFENSIVE CHECK: Prevent crash if appId is null
61+
if (appId == null || appId.isEmpty()) {
62+
Log.e("DatabaseCheck", "Application ID is missing. Kommunicate SDK may not be initialized.");
63+
return false;
64+
}
65+
5966
File dbFile = context.getDatabasePath(dbName);
67+
if (!dbFile.exists()) {
68+
return false; // Database file doesn't exist, so it's not encrypted.
69+
}
6070

61-
// Attempt to open the database with the given password
6271
SQLiteDatabase db = null;
6372
try {
64-
// Corrected method call with byte array for password
73+
// Now it's safe to call getBytes()
6574
db = SQLiteDatabase.openDatabase(dbFile.getPath(), appId.getBytes(), null, SQLiteDatabase.OPEN_READONLY, null, null);
6675
db.close();
67-
return true;
68-
} catch (SQLiteException e) { // Updated exception package
69-
// This exception is thrown if the password is wrong or the DB is not encrypted,
70-
// which for this check, means it's not encrypted with the given key.
76+
return true; // Successfully opened with the key, so it's encrypted.
77+
} catch (SQLiteException e) {
78+
// This is expected if the password (appId) is wrong or DB isn't encrypted.
7179
return false;
7280
} finally {
7381
if (db != null && db.isOpen()) {

kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ object DatabaseMigrationHelper {
3232
val password =
3333
MobiComKitClientService.getApplicationKey(AppContextService.getContext(context))
3434

35+
// Check added to prevent crash in case of null or empty application key
36+
if (password.isNullOrEmpty()) {
37+
System.err.println("Migration failed: Application Key is missing. Is the SDK initialized?")
38+
return
39+
}
40+
3541
// Load SQLCipher libraries
3642
System.loadLibrary("sqlcipher")
3743

0 commit comments

Comments
 (0)