You can easily do this by:
::variableName.isInitialized
or
this::variableName.isInitialized
But if you are inside a listener or inner class, do this:
this@OuterClassName::variableName.isInitialized
Note: The above statements work fine if you are writing them in the same file(same class or inner class) where the variable is declared but this will not work if you want to check the variable of other class (which could be a superclass or any other class which is instantiated), for ex:
class Test {
lateinit var str:String
}
And to check if str is initialized:
What we are doing here: checking isInitialized
for field str
of Test
class in Test2
class.
And we get an error backing field of var is not accessible at this point.
Check a question already raised about this.
File?
) and just check if it is null instead? – Marcin KozińskiallSeries
var toseriesDir?.listFiles()?.map { it.name }?.toTypedArray()
, which is not very "pretty" – Mathew Hanyif (seriesDir != null) {
allSeries = seriesDir.listFiles().map { it.name }.toTypedArray()
}
– Marcin Koziński