Cannot find setter for field - using Kotlin with Room database
Since your fields are marked with val
, they are effectively final and don't have setter fields.
Try switching out the val
with var
.
You might also need to initialize the fields.
@Entity(tableName = "story")
data class Story (
@PrimaryKey var id: Long? = null,
var by: String = "",
var descendants: Int = 0,
var score: Int = 0,
var time: Long = 0L,
var title: String = "",
var type: String = "",
var url: String = ""
)
EDIT
The above solution is a general fix for this error in Kotlin when using Kotlin with other Java libraries like Hibernate where i've seen this as well. If you want to keep immutability with Room, see some of the other answers which may be more specific to your case.
In some cases immutability with Java libraries is simply not working at all and while making sad developer noises, you have to switch that val
for a var
unfortunately.
Hey I don't know if everyone know or not, but you can not have column which is starting from is
into Room
.
For example you can't have like this
@Entity(tableName = "user")
data class User (
@PrimaryKey var id: Long? = null,
var userName: String = "",
var isConnectedToFB: Boolean = false,
)