Looking for a way around this null pointer excepti...
# getting-started
j
Looking for a way around this null pointer exception. This is probably just me using a bad antipattern but I'm not sure how else to do it. I'd like to override the setter for a variable which is used in the superclass, but I get a NullPointerException in the superclass.
Copy code
abstract class Foo(protected open var data: List<Int>) {
    init {
        if (data.isEmpty()) { // NullPointerException!
            println("Empty")
        }
    }
}

class Bar(initialData: List<Int>) : Foo(initialData) {
    override var data = super.data
    	set(value) {
            field = value
            // Do more stuff here
        }
}


fun main() {
    val test = Bar(listOf(1, 2, 3))
}
r
I think you just need to override the getter too
Untitled.cpp
This might be nicer, saves overriding the getter and stops you leaking
this
in the constructor.