title: How to fix 'Cannot create an instance of ViewModel class'?
body: <p>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 <code>java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel</code>.</p>
  
  <p>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.</p>
  
  <p>MainActivity.java </p>
  
  <pre><code>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&lt;List&lt;Note&gt;&gt;() {
          @Override
          public void onChanged(List&lt;Note&gt; notes) {
              Toast.makeText(MainActivity.this, "Updated", Toast.LENGTH_SHORT).show();
          }
      });
  
  }
  }
  </code></pre>
  
  <p>NoteViewModel.java</p>
  
  <pre><code>public class NoteViewModel extends AndroidViewModel {
  
  private NoteRepository noteRepository;
  private LiveData&lt;List&lt;Note&gt;&gt; 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&lt;List&lt;Note&gt;&gt; getAllNotes() {
      return allNotes;
  }
  
  }
  </code></pre>
  
  <p>NoteRepository.java</p>
  
  <pre><code>public class NoteRepository {
  
  private NoteDao noteDao;
  private LiveData&lt;List&lt;Note&gt;&gt; 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&lt;List&lt;Note&gt;&gt; getAllNotes() {
      return allNotes;
  }
  
  private static class InsertNoteAsyncTask extends AsyncTask&lt;Note, Void, Void&gt; {
  
      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&lt;Note, Void, Void&gt; {
  
      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&lt;Note, Void, Void&gt; {
  
      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&lt;Void, Void, Void&gt; {
  
      private NoteDao noteDao;
  
      private DeleteAllNotesAsyncTask(NoteDao noteDao) {
          this.noteDao = noteDao;
      }
  
      @Override
      protected Void doInBackground(Void...voids) {
          noteDao.deleteAllNotes();
          return null;
      }
  }
  
  
  }
  </code></pre>
  
  <p>Log </p>
  
  <blockquote>
    <p>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.(NoteRepository.java:20)
            at com.prathameshmore.getnotes.viewmodel.NoteViewModel.(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.</p>
  </blockquote>