I trying hands on MVVM architecture, I implemented all required classes and methods. While creating an object of ViewModel class in MainActivity, I got this error java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel.

I trying this example from a YouTube tutorial. I did all the implementation correct. I tried on making ViewModel class and constructor public but still, app crashing at runtime.

MainActivity.java

`public class MainActivity extends AppCompatActivity {

private NoteViewModel noteViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
    noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
        @Override
        public void onChanged(List<Note> notes) {
            Toast.makeText(MainActivity.this, "Updated", Toast.LENGTH_SHORT).show();
        }
    });

}

} `

NoteViewModel.java

`public class NoteViewModel extends AndroidViewModel {

private NoteRepository noteRepository;
private LiveData<List<Note>> allNotes;

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

public void insert(Note note) {
    noteRepository.insert(note);
}

public void update(Note note) {
    noteRepository.update(note);
}

public void delete(Note note) {
    noteRepository.delete(note);

}

public void deleteAllNotes() {
    noteRepository.deleteAllNotes();
}

public LiveData<List<Note>> getAllNotes() {
    return allNotes;
}

}`

NoteRepository.java

`public class NoteRepository {

private NoteDao noteDao;
private LiveData<List<Note>> allNotes;

public NoteRepository(Application application) {
    NoteDatabase database = NoteDatabase.getInstance(application);
    noteDao = database.noteDao();
    allNotes = noteDao.getAllNotes();
}

public void insert(Note note){
    new InsertNoteAsyncTask(noteDao).execute(note);
}

public void delete(Note note) {
    new DeleteNoteAsyncTask(noteDao).execute(note);
}

public void update(Note note) {
    new UpdateNoteAsyncTask(noteDao).execute(note);
}

public void deleteAllNotes() {
    new DeleteAllNotesAsyncTask(noteDao).execute();
}

public LiveData<List<Note>> getAllNotes() {
    return allNotes;
}

private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {

    private NoteDao noteDao;

    private InsertNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note...notes) {
        noteDao.insert(notes[0]);
        return null;
    }
}

private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {

    private NoteDao noteDao;

    private UpdateNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note...notes) {
        noteDao.update(notes[0]);
        return null;
    }
}

private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {

    private NoteDao noteDao;

    private DeleteNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note...notes) {
        noteDao.delete(notes[0]);
        return null;
    }
}

private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {

    private NoteDao noteDao;

    private DeleteAllNotesAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Void...voids) {
        noteDao.deleteAllNotes();
        return null;
    }
}

}`

Log

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.prathameshmore.getnotes, PID: 28833 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prathameshmore.getnotes/com.prathameshmore.getnotes.views.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel 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.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6868) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6238)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 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.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25)  at android.app.Activity.performCreate(Activity.java:6868)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6238)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  Caused by: java.lang.RuntimeException: cannot find implementation for com.prathameshmore.getnotes.database.NoteDatabase. NoteDatabase_Impl does not exist at androidx.room.Room.getGeneratedImplementation(Room.java:94) at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:851) at com.prathameshmore.getnotes.database.NoteDatabase.getInstance(NoteDatabase.java:31) at com.prathameshmore.getnotes.repository.NoteRepository.<init>(NoteRepository.java:20) at com.prathameshmore.getnotes.viewmodel.NoteViewModel.<init>(NoteViewModel.java:21) at java.lang.reflect.Constructor.newInstance0(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:430)  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.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25)  at android.app.Activity.performCreate(Activity.java:6868)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6238)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  Application terminated.

1 Answers

0
ismail alaoui On

You need to have a factory class for your ViewModel.

public class MyViewModelFactory implements ViewModelProvider.Factory {
    private Application mApplication;
    private String mParam;


    public MyViewModelFactory(Application application, String param) {
        mApplication = application;
        mParam = param;
    }


    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        return (T) new MyViewModel(mApplication, mParam);
    }
}

And when instantiating the view model, you do like this:

MyViewModel myViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication(), "my awesome param")).get(MyViewModel.class);