val itemList: List<Message>?by messageViewModel.messageList.observeAsState() MessageWindow(itemList = itemList!!)
The error of null pointer is caused by the problem of observeasstate function,
There are two versions of the obserasstate function. One is to give an initial value, and the other is to give no initial value, that is, the version I reported an error.
Because there is no initial value, it will return null!
Because my itemlist starts the background thread to query in the ViewModel, and the main thread runs faster than the background thread, when the main thread runs to the observer line, the data is not ready, the result returns null, and the result conflicts with the following line.
The solution is to use another version of the obser function to give an initial value so that a null error will not be reported.
val itemList: List<Message>?by messageViewModel.messageList.observeAsState(listOf())
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- C Compilation Error: implicit declaration of function xxx is invalid in C99 [-Wimplicit-function-declaration]
- How to Solve Console error: index: 0, size: 0
- Several cases of error reporting in java.lang.nullpointerexception
- [Solved] Vscode css Style Error: webkit-background-clip
- invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’
- This application is modifying the autolayout engine from a background thread, which can lead to engi
- [Solved] An error occurred when paddlepaddle iterated data: typeerror: ‘function’ object is not iterative
- Android studio reports an error using startservice: IllegalStateException
- The use of Hibernate / JPA getsingleresult reports an exception noresult xception:No entity found for query