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?
bohregard
11/11/2021, 8:32 PM
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
Zach Klippenstein (he/him) [MOD]
11/12/2021, 6:47 PM
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
bohregard
11/12/2021, 9:31 PM
I would imagine that’s the case
a
Abhinav Suthar
11/13/2021, 7:53 PM
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