更新 room、kotlin 和 ksp 版本后出现“指定为非空的参数为空”错误

gan*_*aam 5 android kotlin android-room

我通过以下方式使用 room 在 SQLite 数据库中插入一些数据:

在repository类中插入数据的方法:

    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
        }
    }
Run Code Online (Sandbox Code Playgroud)

Dao类中的方法:

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(vararg configurableTypeEntity: ConfigurableTypeEntity)
Run Code Online (Sandbox Code Playgroud)

它一直运行良好。但是将 room 版本从 '2.5.2' 更新到 '2.6.0'、ksp 版本从 '1.8.10-1.0.9' 更新到 '1.9.20-1.0.14' 以及 kotlin 版本从 '1.8.10' 更新到“1.9.20”我收到以下异常:

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)
Run Code Online (Sandbox Code Playgroud)

注意:在日志中,该行是该方法的SyncAllRepository.kt:175行。return trueaddAllConfigurableTypeToDb

我当前的项目级别 build.gradle 如下所示:

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
}
Run Code Online (Sandbox Code Playgroud)

如果我撤消 room 版本、ksp 版本和 kotlin 版本的更改,该项目可以正常运行。但是为什么我使用最新版本后会出现错误?我还应该进行哪些其他更改才能避免出现此异常?

请注意,productCategoryListinaddAllConfigurableTypeToDb()不为空。在 Dao 中insertAll(),我尝试使用 List 而不是 vararg,但仍然得到相同的结果。

小智 0

我遇到了同样的问题并解决了它,将房间编译器依赖项声明从 ksp 更改为 kapt。

编辑:我做了更深入的调查,事实证明我正在使用的房间编译器版本(2.6.1)现在需要将列标记为@Nullable可以假设为空值的值。之后我回滚到 ksp 插件,一切正常。

因此,要解决您的问题,请检查堆栈跟踪生成的房间类中的哪个字段ConfigurableTypeDao_Impl引发异常并将其标记为@Nullable实体类。