kobby opoku
kobby opoku

Reputation: 1

How do I check if an object is null

I have a CrudRepository class that I get employee from by their email address.

val existingEmployee: Employee? = employeeManagementService.findEmployeeByEmail(employeeData.email)

How do i check if existingEmployee returns with a null

Upvotes: 0

Views: 407

Answers (3)

s1m0nw1
s1m0nw1

Reputation: 81929

The documentation is very good and I advise you to read it properly. The nullability topic is discussed here, specifically to answer your question:

val b: String? = "Kotlin"
if (b != null && b.length > 0) {
    print("String of length ${b.length}")
} else {
    print("Empty string")
}

Upvotes: 1

Taseer
Taseer

Reputation: 3502

You can use scoping functions:

existingEmployee?.let { it ->
 //Employee not null
} ?: //employee is null

or go with the old fashion:

 if (existingEmployee != null) { 

 }

Upvotes: 1

mahfuj asif
mahfuj asif

Reputation: 1979

you can try this

existingEmployee?.let { 

    //Do your stuffs

}

Upvotes: 2

Related Questions