9

I am inserting some data in the SQLite Database using room in the following manner:

Method for inserting data in the repository class:

    fun addAllConfigurableTypeToDb(
        allConfigurableTypes: AllConfigurableType,
        database: AppDatabase
    ): Boolean {
        try {
            val productCategoryList = arrayListOf<ConfigurableTypeEntity>()
            for (configurableType in allConfigurableTypes.configurableTypeList) {
                productCategoryList.add(
                    ConfigurableTypeEntity(
                        configurableType.id, configurableType.classType,
                        configurableType.organizationId, configurableType.name,
                        configurableType.code, configurableType.description,
                        configurableType.context, configurableType.isActive,
                        configurableType.isLocked, configurableType.isDeleted,
                        configurableType.createdBy, configurableType.createdByDate,
                        configurableType.lastModifiedBy, configurableType.lastModifiedByDate

                    )
                )
            }
            database.configurableTypeDao.insertAll(*productCategoryList.toTypedArray())
            return true
        } catch (e: Exception) {
            e.printStackTrace()
            return false
        }
    }

The method inside Dao class:

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(vararg configurableTypeEntity: ConfigurableTypeEntity)

It has been working fine. But after updating room version from '2.5.2' to '2.6.0', ksp version from '1.8.10-1.0.9' to '1.9.20-1.0.14' and kotlin version from '1.8.10' to '1.9.20' I'm getting the follwing exception:

12:16:29.832  W  java.lang.NullPointerException: Parameter specified as non-null is null: method androidx.sqlite.db.framework.FrameworkSQLiteProgram.bindString, parameter value
12:16:29.832  W     at androidx.sqlite.db.framework.FrameworkSQLiteProgram.bindString(Unknown Source:3)
12:16:29.832  W     at <project_package_name>.data.local.dao.ConfigurableTypeDao_Impl$1.bind(ConfigurableTypeDao_Impl.java:53)
12:16:29.832  W     at <project_package_name>.data.local.dao.ConfigurableTypeDao_Impl$1.bind(ConfigurableTypeDao_Impl.java:35)
12:16:29.832  W     at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.kt:83)
12:16:29.832  W     at <project_package_name>.data.local.dao.ConfigurableTypeDao_Impl.insertAll(ConfigurableTypeDao_Impl.java:99)
12:16:29.832  W     at <project_package_name>.data.repository.SyncAllRepository.addAllConfigurableTypeToDb(SyncAllRepository.kt:175)
12:16:29.832  W     at <project_package_name>.viewModel.SyncAllViewModel$syncConfigurableTypesTable$1.invokeSuspend(SyncAllViewModel.kt:201)
12:16:29.832  W     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
12:16:29.832  W     at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
12:16:29.832  W     at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
12:16:29.832  W     at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:100)
12:16:29.832  W     at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
12:16:29.832  W     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
12:16:29.832  W     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
12:16:29.832  W     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)

NOTE: in the log, the line SyncAllRepository.kt:175 is the line return true of the addAllConfigurableTypeToDb method.

My currrent project level build.gradle looks like this:

plugins {
    id 'com.android.application' version '8.1.3' apply false
    id 'com.android.library' version '8.1.3' apply false
//    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
    id 'com.google.devtools.ksp' version '1.9.20-1.0.14' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
//    id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
//    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}

If I undo the changes in room version, ksp version and kotlin version, the project works fine. But why am I getting the error after using the latest versions? What other changes should I make so that I don't get this exception?

Please note that the productCategoryList in addAllConfigurableTypeToDb() is not null. In Dao's insertAll() I've tried using List instead of vararg but I still got the same result.

3
  • Have you found any solution? I also faced the same problem, after updating the room version. Commented Jan 6, 2024 at 19:44
  • @AbuNayem unfortunately no. However, when I was working on another personal project, I noticed that I didn't get any of these errors although I was using the same version of KSP, Room, Gradle etc that I mention in this question in that project. So it still baffles me. Commented Jan 8, 2024 at 5:30
  • @AbuNayem one of my hypothesis is that there might have been something wrong with how Kotlin was installed in my computer. But I never tested it out. You can try checking it. Commented Jan 8, 2024 at 5:35

2 Answers 2

2

I had the same problem and solved it changing the room-compiler dependency declaration from ksp to kapt.

Edit: I've made a deeper investigation, turns out in my case is that the room-compiler version I'm using (2.6.1) now requires to mark the column as @Nullable for values that can assume a null value. After that I rolled back to ksp plugin and all works fine.

So, to resolve your problem, check on the stacktrace which field in generated room class ConfigurableTypeDao_Impl is throwing the exception and mark it as @Nullable on it's entity class.

Sign up to request clarification or add additional context in comments.

5 Comments

I am facing the same issue. I had no problems with kapt, issue occurred after changing to ksp. Still trying to find out what causes it before I change back.
I've made a deeper investigation, turns out in my case is that the room-compiler version I'm using (2.6.1) now requires to mark the collumn field as @Nullable for values that can assume a null value. After that I rollback to ksp plugin and all works fine.
Thank you for your investigation. In my case it's a mixed Java / kotlin project and the entities in question are still java classes. The stack trace was not available to me, as the issue happened in production. Added crashlytics record exception in order to find out the cause now. Surprisingly this happens only on some devices, input data seems to be identical (same values null), otherwise it would have appeared while testing. I'll add my findings as soon I get stack traces, and will check if your suggestions work. Thanks a lot, again
If you use TypeConverters, you also need to add @Nullable to them
And if your DAOs are on JAVA, then you need to add @Nullable to method parameters
0

One possible issue could be that after androidx.sqlite sources have been converted from Java to Kotlin in 2.3.0, the nullability handling might be stricter, so make sure not to pass a null parameter in the query where it's not expected to be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.