lateinit var aLiveData: LiveData<List<..>>
aLiveData = // define live data
val list = aLiveData.value
k
KayCee
12/14/2020, 5:58 AM
thank you, of course I did define that in my code. The point to focus is why the line "val list = aLiveData.value" can cause crash
a
akatkov
12/14/2020, 6:04 AM
If you call
aLiveData.value
before you assign
aLiveData = ...
Then it'll be null and crash
akatkov
12/14/2020, 6:06 AM
If
aLiveData
isn't guaranteed to be initialized before you use it, you should make it nullable instead
👍 1
k
KayCee
12/14/2020, 7:26 AM
aLiveData = getListFromDb()
yeah, thats right but in that case the " lateinit exception" will be presented not NPE? since there is no lateinit exception, so I think in my code the "getListFromDb()" return a null livedata? That seems unreasonable with the query:
Copy code
@Query("SELECT * FROM table")
fun getListFromDb(): LiveData<List<DataA>>