Problem
I'm checking for null, but receiving this error:
// testNullCheck.kt
fun isNullOrEmpty(list: List<Int>?): Boolean {
if (list.isEmpty() || list == null ) {
return true
}else {
return false
}
}
fun main(args: Array<String>) {
val test: Boolean = isNullOrEmpty(
listOf<Int>()
)
println(test)
}
Compilation output:
leetcode/kotlin [master●] » kotlinr testNullCheck.kt
Compiling, please wait...
testNullCheck.kt:2:11: error: only safe (?.) or non-null asserted (fg.) calls are allowed on a nullable receiver of type List<Int>?
if (list.isEmpty() || list == null ) {
^
1
2
Questions:
- Why is the compiler complaining?
- Does the order of if-statement operands matter?