Here is my code
class BookmarkViewModel(app: Application) : AndroidViewModel(app) {
private val dao = BookmarkDb.get(app).bookmarkDao()
companion o
Change viewModel = ViewModelProviders.of(activity!!).get(BookmarkViewModel::class.java)
to viewModel = ViewModelProviders.of(this).get(BookmarkViewModel::class.java)
Furthermore don't instantiate the viewModel
to null
. Change it to a lateinit var
this way you don't have to instantiate immediately (but you are telling Kotlin that you will instantiate it before accessing it). You can do this like so: private lateinit var viewModel: BookMarkViewModel
EDIT The root of the problem was that the Room Dependencies
where either not on the same version or annotationProcessor
was used instead of kapt
(kapt
is required when using Kotlin)