Search code examples
kotlinkotlin-null-safety

How do I run a block of code if a nullable type is null?


In Kotlin, I can run code if an object is not null like this:

data?.let {
    // execute this block if not null
}

But how can I execute a block of code if the object is null?


Solution

  • Just use a normal if:

    if (data == null) {
      // Do something
    }