Why do I get the information Enum argument can be null in Java, but exhaustive when contains no null branch in Kotlin?

Viewed 2752

Code B works well, but the Code A get the following warning prompt (It can run), why?

Enum argument can be null in Java, but exhaustive when contains no null branch

I think _playState.value can be null in Code B too, do I should add null branch when I use exhaustive when in Code B?

Code A

private val _selectAction = MutableLiveData<ESelect>()
val selectAction: LiveData<ESelect> = _selectAction

selectAction.observe(mLifecycleOwner) {
   when(it) {               //Enum argument can be null in Java, but exhaustive when contains no null branch                                   
         ESelect.SelectAll    -> binding.chSelect.isChecked = true
         ESelect.UnselectAll  -> binding.chSelect.isChecked = false
         ESelect.NoAction     -> Unit
   }
}



enum class ESelect{
    SelectAll,
    UnselectAll,
    NoAction  
}

Code B

private val _playState=MutableLiveData<EPlayState>()

 override fun playOrPause(filename: String) {
        when (_playState.value) {
           EPlayState.STOPPED -> play(filename)
           EPlayState.PLAYING -> pause()
           EPlayState.PAUSED  -> resume()
        }
    }


enum class EPlayState {
    STOPPED,
    PLAYING,
    PAUSED
}
1 Answers

LiveData is written in Java. The second parameter that you pass to the observe() function is an anonymous implementation of Java interface Observer. Since Java doesn't know anything about nullability in Kotlin, the parameter it assumed to be nullable.

Related