Kotlin / Room错误:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数消息

时间:2019-01-08 17:26:27

标签: android kotlin android-room

不确定是什么原因造成的,但是我正在尝试从api请求数据,该api包含Message对象的数组。如果我将结果打印到控制台,则当我希望Messages=null是对象数组时,除Message之外的数据都是正确的。我不明白我错过了什么?

我收到此错误: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter messages

有人能指出我正确的方向吗?该类的以下代码:

const val PROFILE_RESPONSE_ID = 0

@Entity(tableName = "profile")
data class ProfileResponse(
    val id: Int,
    val name: String,
    val code: String,
    val title: String,
    @SerializedName("profile_image")
    val profileImage: String,
    @SerializedName("background_image")
    val backgroundImage: String,
    @Embedded(prefix = "messages_")
    val messages: ArrayList<Messages>,
) {
    @PrimaryKey(autoGenerate = false)
    var responseId: Int = PROFILE_RESPONSE_ID
}

示例JSON:

{
  "id": 44,
  "name": "Jason",
  "code": "jason",
  "title": "Jason Scott",
  "profile_image": "https://sampleurl.com/sample_profile.jpg",
  "background_image": "",
  "messages": [
    {
      "id": 0001,
      "message": "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.",
      "timestamp": "Thu, 01 Jan 1970 01:00:00 +0100",
    }
    {
      "id": 0002,
      "message": "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.",
      "timestamp": "Thu, 01 Jan 1970 01:00:00 +0100",
    }
    {
      "id": 0003,
      "message": "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.",
      "timestamp": "Thu, 01 Jan 1970 01:00:00 +0100",
    }
}

1 个答案:

答案 0 :(得分:0)

设法通过删除@Embedded来解决此问题,因为我不一定需要另一个表中的数据,并添加了TypeConverter将该列表转换为String并返回(此question帮助了)。供其他人参考:

TypeConvertor类

class Convertors {

    @TypeConverter
    fun listToJson(value: List<Message>?): String {
        return Gson().toJson(value)
    }

    @TypeConverter
    fun jsonToList(value: String): List<Message>? {
        val objects = Gson().fromJson(value, Array<Message>::class.java) as Array<Message>
        val list = objects.toList()
        return list
    }

}

然后将@TypeConverters添加到我的数据库类中。

数据库类

@Database(entities = [ProfileResponse::class], version = 1)
@TypeConverters(Convertors::class)
abstract class MainDatabase: RoomDatabase() {

    abstract fun profileResponseDao(): ProfileResponseDao

}