What does the !! (double exclamation mark) operator do in Kotlin and when should it be used?
Hello everyone! I just started learning Kotlin after Java and can't figure out the `!!` operator. Kotlin is praised for null safety, but here's some double exclamation mark that, as I understand, on the contrary, can cause a NullPointerException.
I'm writing a small notes application. I have a function that searches for a user by ID in the database and returns their name. If the user is not found, it returns null. Here's an example code:
```kotlin
fun getUserName(userId: Int): String? {
// returns null if user not found
return database.findUser(userId)?.name
}
```
Then I call this function and want to display the name in a TextView:
```kotlin
val name = getUserName(123)
textView.text = name // Error: Type mismatch. Required: String, Found: String?
```
No answers yet.
Log in to leave an answer
Blogs
Asynchronous Programming in Java: Complete Guide with Examples