Jakub Komorowski
09/27/2019, 12:13 PMsetEnabled
method. I’ve overridden it in my class, and used properties from the child class in this overwrite. As the properties I’ve used were unmutable and non-nullable I was startled when my app crashed with NullPointerException
thrown in that method. Turns out it was called from the superclass constructor, and properties are initialized only after superclass constructor call. The simplified code that caused my problem looks something like: https://bit.ly/2n6xmm5 . When you run it, it throws NullPointerException
when using seemingluynon-nullable property.
Is there a pattern that would allow us to omit method calls on unitialized unmutable properties? I found two solutions to that problem, but I don’t think they’re very elegant. One using lateinit vars https://bit.ly/2nPYBBX - pretty ugly, and one using a flag https://bit.ly/2mgG4hq - pretty weird.
All of the urls are shortened https://play.kotlinlang.org code snippets.Iaroslav Postovalov
09/27/2019, 12:25 PMJakub Komorowski
09/27/2019, 12:27 PMIaroslav Postovalov
09/27/2019, 12:30 PMJakub Komorowski
09/27/2019, 12:50 PMlateinit
properties, and, as can be seen in the snippet I provided, converting properties to those looks pretty ugly.streetsofboston
09/27/2019, 1:14 PMisInitialized
is usually a code-smell. Use it very sparingly..
Either use nullable types ?
or use the by lazy { .... }
(and provide the right dependencies for the code in the lazy-block to work).Jakub Komorowski
09/27/2019, 1:19 PMstreetsofboston
09/27/2019, 1:28 PMsetEnabled(isEnabled)
in the constructor of the base-class BaseView
. The sub-class DerivedView
has not yet been instantiated at all.Jakub Komorowski
09/27/2019, 1:30 PMDico
09/27/2019, 5:06 PMval initialized = true
override fun setEnabled(...) {
super.setEnabled...
if (initialized)...