[英]Android Room Migration - java.lang.IllegalArgumentException: Parameter specified as non-null is null
I am trying to add a column to an existing table in the database. 我正在尝试向数据库中的现有表添加一列。 The column is type
ArrayList<Date>
I wrote a migration step, increased database version number, and added the migration. 该列的类型为
ArrayList<Date>
我编写了一个迁移步骤,增加了数据库版本号,并添加了迁移。 When I run the migration (run the app), I get the error: 当我运行迁移(运行应用程序)时,出现错误:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter value
java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数值
when attempting to read data from that table. 尝试从该表读取数据时。
I have tried following the steps here: https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929 我尝试按照以下步骤操作: https : //medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929
I have also tested with other data types, and it has migrated successfully. 我还对其他数据类型进行了测试,并且已成功迁移。 Seems like the issue is adding Lists or
ArrayList
s. 似乎问题出在添加List或
ArrayList
。
val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"ALTER TABLE product ADD COLUMN ingredients TEXT"
)
}
}
You're altering a table without setting a default value. 您正在更改表而未设置默认值。 Therefore the default is null for all rows already present within the table.
因此,对于表中已经存在的所有行,默认值为null。 Since you require the Kotlin property to be non-null, you receive the
IllegalArgumentException
. 由于您要求Kotlin属性为非null,因此会收到
IllegalArgumentException
。
To resolve the issue, you'd have to alter the table to contain a default value for ingredients
and declare it non-null as well. 要解决此问题,您必须更改表以包含
ingredients
的默认值,并将其声明为非null。
val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"ALTER TABLE product ADD COLUMN ingredients TEXT NOT NULL DEFAULT ''"
)
}
}
Alternatively you could make the ingredients
Kotlin property nullable. 或者,您可以使
ingredients
Kotlin属性为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.