Reputation: 1
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
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
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