Search code examples
androidkotlincrashandroid-roomkotlin-null-safety

Android Room returns Null as Non-Null type


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:

  1. How is possible that Room can return Null value as Non-Null variable?
  2. How is possible that code with assign null to Non-Null variable has no crashes?

Solution

  • 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.