I have Dao
to return simple object. If object does not exist, Room return null
, but Android app have no crashes. Also if I assign that value to non-null variable, no crashes are in app.
Dao:
@Query("SELECT * FROM users WHERE id LIKE :id LIMIT 1")
abstract fun getById(id: Long): User
not crashing code:
doAsync {
val user: User = userDao.getById(999) // user 999 not exist, userDao returns null
uiThread {
if (user == null) {
Timber.d("user is $user") // "user is null" in log
} else {
Timber.d("user is ${user.email}")
}
}
}
I have two questions:
The reason you're not getting a NullPointerException
is simple: Room is a Java library, not Kotlin, so it has no idea your User
should not be null.
When interfacing with Java code, Kotlin introduces some null checks to give you that exception, but since your interface is written in Kotlin it assumes that won't be a problem and skips the check.
I'm not a Room expert, but after a quick google search, I couldn't find a way of forcing Room to check for nulls. I see two ways of solving your "problem":
getById
function to return User?
@Dao
to a Java interface, forcing Kotlin to check for null.It's all about how Kotlin handle nulls on boundaries with Java code.
If you pass null from Java to Kotlin method that require non-null value you will get exception. This is achieved with calls to Intrinsics.checkNotNull
function added by Kotlin compiler in beginning of method.
For example:
fun hello(who: String): Unit {
println ("Hello $who")
}
becomes
public final void hello(@NotNull String who) {
Intrinsics.checkParameterIsNotNull(who, "who");
String var2 = "Hello " + who;
System.out.println(var2);
}
Similar check added when you call Java methods from Kotlin.
But in you case you have Kotlin interface and it's implementation in Java generated by Room. So Kotlin compiler can't add checks because it has no control on all implementations of interface. Otherwise it would have to add checks after every call to Kotlin class or interface because it can be implemented in Java, what is bad for performance.
UPD: found similar issue at Room bugtracker https://issuetracker.google.com/issues/112323132. Googler said that this is intended behavior and if you wrote query that can return null value, then you are responsible to mark it as nullable in dao interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With