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}")
}
}
}
我有两个问题:
- How is possible that Room can return Null value as Non-Null variable?
- How is possible that code with assign null to Non-Null variable has no crashes?