-1

In my MVVM app, I am extending AndroidViewModel and through a Repository class, I am trying to get an instance of a DB (in a separate abstract class that extends the RoomDatabase superclass). I call the ViewModel from the MainActivity class by the ViewModelProviders method. I get the following error:

Here is the whole stack trace of the exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.roomlistingapllicate, PID: 31766
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roomlistingapllicate/com.example.roomlistingapllicate.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.roomlistingapllicate.NOtesViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5728)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.roomlistingapllicate.NOtesViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:238)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
        at com.example.roomlistingapllicate.MainActivity.onCreate(MainActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:6309)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
        at android.app.ActivityThread.access$900(ActivityThread.java:175) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5728) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.example.roomlistingapllicate.MainActivity.onCreate(MainActivity.java:19) 
        at android.app.Activity.performCreate(Activity.java:6309) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
        at android.app.ActivityThread.access$900(ActivityThread.java:175) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5728) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
     Caused by: java.lang.RuntimeException: cannot find implementation for com.example.roomlistingapllicate.NotesDatabase. NotesDatabase_Impl does not exist
        at androidx.room.Room.getGeneratedImplementation(Room.java:94)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:851)
        at com.example.roomlistingapllicate.NotesDatabase.getInstance(NotesDatabase.java:23)
        at com.example.roomlistingapllicate.NoteRepository.<init>(NoteRepository.java:15)
        at com.example.roomlistingapllicate.NOtesViewModel.<init>(NOtesViewModel.java:15)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.example.roomlistingapllicate.MainActivity.onCreate(MainActivity.java:19) 
        at android.app.Activity.performCreate(Activity.java:6309) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
        at android.app.ActivityThread.access$900(ActivityThread.java:175) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5728) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

I am working on the AndroidX arch. Here are some parts of the code I think the issue hides in:

import androidx.appcompat.app.AppCompatActivity;

import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    private NOtesViewModel nOtesViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nOtesViewModel = ViewModelProviders.of(this).get(NOtesViewModel.class);

    }
}


import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import java.util.List;

public class NOtesViewModel extends AndroidViewModel {
    private NoteRepository noteRepository;
    private LiveData<List<Note>> allNotes;

    public NOtesViewModel(@NonNull Application application) {
        super(application);
        noteRepository = new NoteRepository(application);
        allNotes = noteRepository.getAllNotes();
    }


import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class NoteRepository {
    private NoteDAO noteDAO;
    private LiveData<List<Note>> allNotes;

    public NoteRepository(Application application) {
        NotesDatabase notesDatabase = NotesDatabase.getInstance(application.getApplicationContext());
        noteDAO = notesDatabase.noteDAO();
        allNotes = noteDAO.getAllNotes();
    }


import android.content.Context;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;

@Database(entities = {Note.class}, version = 1)
public abstract class NotesDatabase extends RoomDatabase {

    private static NotesDatabase instance;
    public abstract NoteDAO noteDAO();

    public static synchronized NotesDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(), NotesDatabase.class,
                    "note_database").fallbackToDestructiveMigration()
                    .addCallback(roomCallback)
                    .build();
        }
        return instance;
    }
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
  • 1
    Possible duplicate of [Android ViewModel additional arguments](https://stackoverflow.com/questions/46283981/android-viewmodel-additional-arguments) – ronginat May 01 '19 at 10:13
  • If there is a duplicate I can't see it. BTW I have tried narrowing the argument like following application.getApplicationContext() but it didn't work (I knew it wont but still...). But yes the error happens when I pass the argument from the Repository file (NoteRepository.class) to the RoomDatabase extended superclass abstract class file: NoteDatabase noteDatabase = NoteDatabase.getInstanceOfTheDB(application), so the getInstanceOfTheDB method request (Context context) as arguments. –  May 01 '19 at 10:32
  • 1
    Can you show us the whole stacktrace of the exception? – JensV May 01 '19 at 10:37
  • @JensV I have edited my answer in adding the stack trace of the exception –  May 01 '19 at 11:01

1 Answers1

2

The stacktrace says you're getting this error because Room can not find an implementation of your Notes Database abstract class.

Please make sure you have added the room annotation processor dependency in your app's build.gradle file.

annotationProcessor "androidx.room:room-compiler:$room_version"

Edit: If you are using Kotlin, use kapt instead of annotationProcessor, and make sure to add apply plugin: 'kotlin-kapt' at top of the app's build.gradle file.

harold_admin
  • 1,117
  • 2
  • 10
  • 20
  • Thanx a lot. I was using androidTestImplementatin instead of annotationProcessor. Problem solved. –  May 01 '19 at 11:21