I have a data class that contains a mutableState p...
# compose
b
I have a data class that contains a mutableState property but when I create the object (through retrofit with a gson serializer) the mutableState property always ends with a the error
"Attempt to invoke interface method 'java.lang.Object androidx.compose.runtime.State.getValue()' on a null object reference
.
Copy code
data class ActivationResponder(
    val id: Long,
    val firstName: String,
    val lastName: String,
    @SerializedName("callSign")
    val callsign: String,
    val phoneNumber: String,
) {

    var isChecked: Boolean by mutableStateOf(false)
    val name: String
    get() = "$firstName $lastName"

}
Any insight on to why that would be the case?
What’s more: If I create an intermediate class that sits between this class and the API call, it seems to work with no issues. So, what would cause retrofit/gson to not properly create the mutableStateOf property on the object?
Copy code
val responders = activateApi.getAvailableResponders(activationId).map {
    ActivationResponder(
        callsign = it.callsign,
        id = it.id,
        firstName = it.firstName,
        lastName = it.lastName,
    )
}
z
I'm guessing gson isn't aware of kotlin delegated properties and doesn't know how to initialize that field when it reflectively creates an instance of your class.
👍 1
b
I would imagine that’s the case
a
Copy code
data class Foo(val x: Any){
val y = "some string"
init{
    Log.d(TAG, message)
}
}
If you create object of this class using gson then the log statement will not be printed. It seems like that class object created with gson doesn't calls init fun and so isChecked is not initialised. It has nothing to do with
by
delegate. Even
y
variable will be null.
168 Views