Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 609 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if a "lateinit" variable has been initialized?

#1
I wonder if there is a way to check if a `lateinit` variable has been initialized. For example:

class Foo() {

private lateinit var myFile: File

fun bar(path: String?) {
path?.let { myFile = File(it) }
}

fun bar2() {
myFile.whateverMethod()
// May crash since I don't know whether myFile has been initialized
}
}
Reply

#2
Try to use it and you will receive a `UninitializedPropertyAccessException` if it is not initialized.

`lateinit` is specifically for cases where fields are initialized after construction, but before actual use (a model which most injection frameworks use).
If this is not your use case `lateinit` might not be the right choice.

EDIT: Based on what you want to do something like this would work better:

val chosenFile = SimpleObjectProperty<File?>
val button: Button

// Disables the button if chosenFile.get() is null
button.disableProperty.bind(chosenFile.isNull())
Reply

#3
kotlin.UninitializedPropertyAccessException: lateinit property clientKeypair has not been initialized

Bytecode says...blah blah..


public final static synthetic access$getClientKeypair$p(Lcom/takharsh/ecdh/MainActivity;)Ljava/security/KeyPair;

`L0
LINENUMBER 11 L0
ALOAD 0
GETFIELD com/takharsh/ecdh/MainActivity.clientKeypair : Ljava/security/KeyPair;
DUP
IFNONNULL L1
LDC "clientKeypair"
INVOKESTATIC kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException (Ljava/lang/String;)V
L1
ARETURN
L2
LOCALVARIABLE $this Lcom/takharsh/ecdh/MainActivity; L0 L2 0
MAXSTACK = 2
MAXLOCALS = 1

Kotlin creates an extra local variable of same instance and check if it null or not, if null then throws 'throwUninitializedPropertyAccessException' else return the local object.
Above bytecode explained [here][1]
Solution
Since kotlin 1.2 it allows to check weather lateinit var has been initialized or not using `.isInitialized`


[1]:

[To see links please register here]

Reply

#4
[Accepted answer][1] gives me a compiler error in `Kotlin 1.3+`, I had to explicitly mention the `this` keyword before `::`. Below is the working code.

lateinit var file: File

if (this::file.isInitialized) {

// file is not null
}


[1]:

[To see links please register here]

Reply

#5
There is a `lateinit` improvement in Kotlin 1.2 that allows to check the initialization state of `lateinit` variable directly:

lateinit var file: File

if (this::file.isInitialized) { ... }

See the annoucement on [JetBrains blog][1] or the [KEEP proposal][2].


**UPDATE:** Kotlin 1.2 has been released. You can find `lateinit` enhancements here:

- [Checking whether a lateinit var is initialized][3]
- [Lateinit top-level properties and local variables][4]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#6
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:

[![enter image description here][1]][1]

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][2] already raised about this.


[1]:

[2]:

[To see links please register here]

Reply

#7
If you have a `lateinit` property in one class and need to check if it is initialized from another class

if(foo::file.isInitialized) // this wouldn't work

The workaround I have found is to create a function to check if the property is initialized and then you can call that function from any other class.

**Example:**

class Foo() {

private lateinit var myFile: File

fun isFileInitialised() = ::file.isInitialized
}

// in another class
class Bar() {

val foo = Foo()

if(foo.isFileInitialised()) // this should work
}


Reply

#8
Using `.isInitialized` property one can check initialization state of a lateinit variable.


if (::file.isInitialized) {
// File is initialized
} else {
// File is not initialized
}

Reply

#9
This will work

```
if (::list.isInitialized) {
//true
}
else {
//false
}
```
Reply

#10
### Checking lateinit var

To check whether a `lateinit var` was initialized or not, simply use an `.isInitialized` boolean on the property reference `::` .


if (foo::bar.isInitialized) {
println(foo.bar)
}

Kotlin Playground's code may look like this:

fun main() {
var declarative = Declarative()
declarative.checkLateInit()
}

class Declarative {
lateinit var compose: String

fun checkLateInit() {
println(this::compose.isInitialized)
compose = "Jetpack Compose 1.4"

if (this::compose.isInitialized) {
println(this.compose)
}
}
}

// Result:

// false
// Jetpack Compose 1.4

This checking is only available for the properties that are accessible lexically, i.e. declared in the same type or in one of the outer types, or at top level in the same file.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through