Getting Error While Dealing With Getter And Setter In Kotlin
Solution 1:
You call the setter inside of the setter.. a.k.a. infinite loop:
set(value) {
/* execute setter logic */
chatManger = value
}
Inside a property getter or setter there is an additional variable available: field
. This represents the java backing field of that property.
get() = field
set(value) {
field = value
}
With a regular var property, these getters and setters are auto-generated. So, this is default behaviour and you don't have to override getter / setter if all you do is set the value to a field.
Solution 2:
It is important to remember that referring to chatManger
ANYWHERE in the code ends up calling getChatManger()
or setChatManger()
, including inside of the getter or setter itself. This means your code will end up in an infinite loop and cause a StackOverflowError
.
Read up on Properties, specifically the section about getters/setters as well as the "backing field".
Post a Comment for "Getting Error While Dealing With Getter And Setter In Kotlin"