I have a data-class with an enum property. I also wrote a delegate which gives me that property as a string.
data class POJO (val id: Int, val status: Status) {
val statusStr by enumValue { status }
enum class Status {
TODO, DONE, STARTED
}
}
fun <T : Enum<T>> enumValue(propFn: () -> T): ReadOnlyProperty<Any, String> =
object : ReadOnlyProperty<Any, String> {
override fun getValue(thisRef: Any, property: KProperty<*>)
= propFn().name
}
First this works as intended, but when I deserialize the class from JSON I get an NPE when accessing the property:
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlin.properties.ReadOnlyProperty.getValue(java.lang.Object, kotlin.reflect.KProperty)' on a null object reference
I figured it was because I’m actually the constructor value in the lambda. I have also tried:
data class POJO (val id: Int, val status: Status) {
val statusStr by enumValue { this@Reklamation.status }
}
fun <T : Enum<T>> enumValue(propFn: Any.() -> T): ReadOnlyProperty<Any, String> =
object : ReadOnlyProperty<Any, String> {
override fun getValue(thisRef: Any, property: KProperty<*>)
= thisRef.propFn().name
}
I know there are other ways to solve this, but I don’t understand why it doesn’t work.