So I run into this problem quite often lately, most of the time it happens when I have defined a Hibernate Entity in Kotlin, and I wish to use it in my code.
It happens when there's a property that could conceivably be accessed in another thread, cause the two statements, the if-statement and the execution statement to look at different states, and cause a ClassCastException.
The following code demonstrates the message Kotlin provides when writing such a program:
Possible solutions
References
- Youtube - Let, Also, Apply, Run, With - Kotlin Scope Functions
- https://www.youtube.com/watch?v=Vy-dS2SVoHk