0

My ViewModel class is throwing a RunTimeException. It basically cannot create an instance of the ViewModel class. Here is the stacktrace:

2019-05-29 01:27:56.700 9698-9698/com.example.cryptpass_kt E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cryptpass_kt, PID: 9698
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cryptpass_kt/com.example.cryptpass_kt.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.cryptpass_kt.EntryViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2853)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6703)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.cryptpass_kt.EntryViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:208)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103)
        at com.example.cryptpass_kt.MainActivity.onCreate(MainActivity.kt:40)
        at android.app.Activity.performCreate(Activity.java:6980)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6703) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:200)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) 
        at com.example.cryptpass_kt.MainActivity.onCreate(MainActivity.kt:40) 
        at android.app.Activity.performCreate(Activity.java:6980) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6703) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 
     Caused by: java.lang.RuntimeException: cannot find implementation for com.example.cryptpass_kt.EntryDatabase. EntryDatabase_Impl does not exist
        at androidx.room.Room.getGeneratedImplementation(Room.java:94)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:667)
        at com.example.cryptpass_kt.EntryDatabase$Companion.getInstance(EntryDatabase.kt:17)
        at com.example.cryptpass_kt.EntryRepository.<init>(EntryRepository.kt:12)
        at com.example.cryptpass_kt.EntryViewModel.<init>(EntryViewModel.kt:8)

The error points to 4 different locations in the app, which are all rooted in the same problem. For example, these lines in the onCreate() method of my MainActivity.kt are in the stacktrace:

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    viewModel = ViewModelProviders.of(this).get(EntryViewModel::class.java)
    viewModel.allEntries.observe(this, Observer { entries -> adapter.submitList(entries) })

    ...
}

I've followed some of the suggestions in SO, such as initializing the ViewModel object as lateinit and not null but so far it hasn't made any changes to the situation

EntryViewModel.kt

class EntryViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: EntryRepository = EntryRepository(application)
    val allEntries: LiveData<List<Entry>>

    init {
        allEntries = repository.allEntries!!
    }

    fun insert(entry: Entry) {
        repository.insert(entry)
    }

    fun update(entry: Entry) {
        repository.update(entry)
    }

    fun delete(entry: Entry) {
        repository.update(entry)
    }

    fun deleteAll() {
        repository.deleteAllEntries()
    }
}

EntryDatabase.kt

@Database(entities = [Entry::class], version = 1)
abstract class EntryDatabase : RoomDatabase() {

    abstract fun entryDAO(): EntryDAO

    companion object{
        private var instance: EntryDatabase? = null

        @Synchronized
        fun getInstance(context: Context): EntryDatabase{
            if(instance == null){instance = Room.databaseBuilder<EntryDatabase>(context.applicationContext, EntryDatabase::class.java, "entry_database").fallbackToDestructiveMigration().build()}

            return instance as EntryDatabase
        }
    }
}

And I have all of the necessary gradle dependencies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha05'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.android.support:design:28.1.0'

    implementation "android.arch.lifecycle:extensions:2.2.0-alpha01"
    implementation "android.arch.persistence.room:runtime:2.1.0-beta01"
    implementation "android.arch.paging:runtime:2.1.0-beta01"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'io.sentry:sentry-android:1.7.16'
    implementation 'org.slf4j:slf4j-nop:1.7.25'
}

2 Answers2

1

In the stacktrace you posted exceptions were put one in another. So the last one is main root of the problem:

Caused by: java.lang.RuntimeException: cannot find implementation for com.example.cryptpass_kt.EntryDatabase. EntryDatabase_Impl does not exist
at androidx.room.Room...

As I can see from stacktrace, there is error while you trying to get instance of your database. I don't think it is good idea to get database reference in ViewModel constructor, but it's not essential.

About your database code: there are some possible reasons.

1. Dependencies.

As I saw in other answers from similar questions (I think, this one is the best) you should add some gradle dependencies:

// Extensions = ViewModel + LiveData
implementation "android.arch.lifecycle:extensions:1.1.0"
kapt "android.arch.lifecycle:compiler:1.1.0"
// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"

If I get it right, Kotlin just need some additional dependencies (use keyword kapt instead of implementation for it).

2. Annotations.

Another possible reason: you didn't place Entity annotation to your Entry class (as said in this answer). Another needed annotation is Database, but you already have it.

Ircover
  • 2,406
  • 2
  • 22
  • 42
1

Try with this

val viewModel = ViewModelProviders.of(this).get(EntryViewModel::class.java)

and in your app build.gradle

    implementation 'android.arch.lifecycle:extensions:1.1.1'
Ferran
  • 1,442
  • 1
  • 6
  • 10