I'm getting a RARE null pointer exception in the following code:
class Artist {
fun draw(canvas: Canvas, state: State){
state.drawableObjects.forEach {
it.draw(canvas) //NULL POINTER!?! Can not call draw(Canvas) on null object
}
}
}
class State {
var drawableObjects = listOf<DrawableObject>
set(value) {
field = value.filter { it.isVisible }
}
}
class DrawableObject {
val isVisible = true
fun draw(canvas: Canvas) { }
}
How is this possible? The list drawableObjects is immutable. It also does not allow null objects. When the list is changed an entirely new list is set to prevent modification during the draw call.
I should definitely mention that multiple threads are involved. 2 threads only. One calling Artist.draw() and a second calling State.drawableObjects = listOf()
from How in Kotlin on Android can a null pointer exception occur in an immutable list during a or each loop?
No comments:
Post a Comment