title: Cannot create an instance of ViewModel Class
body: <p>When I try to run my app, it crashes with this logcat error. This problem is very similar to <a href="https://stackoverflow.com/questions/57982050/cannot-create-an-instance-of-class-com-example-architectureexample-noteviewmodel?noredirect=1&amp;lq=1">this</a> and <a href="https://stackoverflow.com/questions/57982050/cannot-create-an-instance-of-class-com-example-architectureexample-noteviewmodel?noredirect=1&amp;lq=1">this</a> post but all mentioned solutions didn't work for me.</p>
  <p>When I searched for this error, I found out that it's usually because of the viewmodelproviders being outdated and people recommending to use the new ViewModelProvider with Factory class but even that didn't work out for me.</p>
  <p><code>java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel</code></p>
  <p>I don't see where there could be an issue, if I have a tiny syntax error somewhere or something else. It is driving me crazy as I'm a beginner and trying to learn Android Architecture Components. I was following a youtube playlist linked <a href="https://www.youtube.com/playlist?list=PLrnPJCHvNZuDihTpkRs6SpZhqgBqPU118" rel="nofollow noreferrer">here</a> and his code worked flawlessly when he ran it. Please take a look at it before flagging it as a duplicate. I'm really out of my depth here. Thanks in advance.</p>
  <p><strong>logcat error</strong></p>
  <pre><code>    Process: com.emirhalici.notetakingappnvvmexample, PID: 17577
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emirhalici.notetakingappnvvmexample/com.emirhalici.notetakingappnvvmexample.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:223)
      at android.app.ActivityThread.main(ActivityThread.java:7656)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
   Caused by: java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel
      at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:275)
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
      at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26)
      at android.app.Activity.performCreate(Activity.java:8000)
      at android.app.Activity.performCreate(Activity.java:7984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:223) 
      at android.app.ActivityThread.main(ActivityThread.java:7656) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.newInstance0(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
      at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267)
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
      at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26) 
      at android.app.Activity.performCreate(Activity.java:8000) 
      at android.app.Activity.performCreate(Activity.java:7984) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:223) 
      at android.app.ActivityThread.main(ActivityThread.java:7656) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
   Caused by: java.lang.RuntimeException: cannot find implementation for com.emirhalici.notetakingappnvvmexample.NoteDatabase. NoteDatabase_Impl does not exist
      at androidx.room.Room.getGeneratedImplementation(Room.java:97)
      at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
      at com.emirhalici.notetakingappnvvmexample.NoteDatabase.getInstance(NoteDatabase.java:25)
      at com.emirhalici.notetakingappnvvmexample.NoteRepository.&lt;init&gt;(NoteRepository.java:15)
      at com.emirhalici.notetakingappnvvmexample.NoteViewModel.&lt;init&gt;(NoteViewModel.java:19)
      at java.lang.reflect.Constructor.newInstance0(Native Method) 
      at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
      at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267) 
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
      at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
      at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26) 
      at android.app.Activity.performCreate(Activity.java:8000) 
      at android.app.Activity.performCreate(Activity.java:7984) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:223) 
      at android.app.ActivityThread.main(ActivityThread.java:7656) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
  </code></pre>
  <p><strong>MainActivity.java</strong></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);
  
      // these didn't work
      //noteViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
      //noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
      //noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
      // i tried this but this don't work either
      noteViewModel = new ViewModelProvider(this,
              ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
      noteViewModel.getAllNotes().observe(this, new Observer&lt;List&lt;Note&gt;&gt;() {
          @Override
          public void onChanged(List&lt;Note&gt; notes) {
              // update RecyclerView
              Toast.makeText(MainActivity.this, &quot;onChanged&quot;, Toast.LENGTH_SHORT).show();
          }
      });
  } }
  </code></pre>
  <p><strong>Note.java (entity class)</strong></p>
  <pre><code>@Entity(tableName = &quot;note_table&quot;)
  public class Note {
  
  @PrimaryKey(autoGenerate = true)
  private int id;
  
  private String title;
  private String description;
  private int priority;
  
  // getters
  public int getId() {
      return id;
  }
  
  public String getTitle() {
      return title;
  }
  
  public String getDescription() {
      return description;
  }
  
  public int getPriority() {
      return priority;
  }
  
  // id setter
  public void setId(int id) {
      this.id = id;
  }
  
  // constructor
  public Note(String title, String description, int priority) {
      this.title = title;
      this.description = description;
      this.priority = priority;
  }
  
  }
  </code></pre>
  <p><strong>NoteDao.java</strong></p>
  <pre><code>@Dao
  public interface NoteDao {
  
  @Insert
  void insert(Note note);
  
  @Update
  void update(Note note);
  
  @Delete
  void delete(Note note);
  
  @Query(&quot;DELETE FROM note_table&quot;)
  void deleteAll();
  
  @Query(&quot;SELECT * FROM note_table ORDER BY priority DESC&quot;)
  LiveData&lt;List&lt;Note&gt;&gt; getAllNotes();
  
  }
  </code></pre>
  <p><strong>NoteDatabase.java</strong></p>
  <pre><code>@Database(entities = Note.class, version = 1)
  public abstract class NoteDatabase extends RoomDatabase {
  
  private static NoteDatabase instance;
  
  public abstract NoteDao noteDao();
  
  public static synchronized NoteDatabase getInstance(Context context) {
      if (instance == null) {
          instance = Room.databaseBuilder(context.getApplicationContext(),
                  NoteDatabase.class, &quot;note_database&quot;)
                  .fallbackToDestructiveMigration()
                  .addCallback(roomCallback)
                  .build();
      }
      return instance;
  }
  
  private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
      @Override
      public void onCreate(@NonNull SupportSQLiteDatabase db) {
          super.onCreate(db);
          new PopulateDbAsyncTask(instance).execute();
      }
  };
  
  private static class PopulateDbAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {
      private NoteDao noteDao;
      private PopulateDbAsyncTask(NoteDatabase db) {
          noteDao = db.noteDao();
      }
  
      @Override
      protected Void doInBackground(Void... voids) {
          noteDao.insert(new Note(&quot;Title 1&quot;,&quot;Description 1&quot;, 1));
          noteDao.insert(new Note(&quot;Title 2&quot;,&quot;Description 2&quot;, 2));
          noteDao.insert(new Note(&quot;Title 3&quot;,&quot;Description 3&quot;, 3));
          noteDao.insert(new Note(&quot;Title 4&quot;,&quot;Description 4&quot;, 1));
          noteDao.insert(new Note(&quot;Title 5&quot;,&quot;Description 5&quot;, 2));
          noteDao.insert(new Note(&quot;Title 6&quot;,&quot;Description 6&quot;, 4));
          return null;
      }
  }
  }
  </code></pre>
  <p><strong>NoteRepository.java</strong></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 update(Note note) {
      new UpdateNoteAsyncTask(noteDao).execute(note);
  }
  
  public void delete(Note note) {
      new DeleteNoteAsyncTask(noteDao).execute(note);
  }
  
  public void deleteAll() {
      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;Note, Void, Void&gt; {
      private NoteDao noteDao;
  
      private DeleteAllNotesAsyncTask(NoteDao noteDao) {
          this.noteDao = noteDao;
      }
  
      @Override
      protected Void doInBackground(Note... voids) {
          noteDao.deleteAll();
          return null;
      }
  }
  }
  </code></pre>
  <p><strong>NoteViewModel.java</strong></p>
  <pre><code>public class NoteViewModel extends AndroidViewModel {
  private NoteRepository repository;
  private LiveData&lt;List&lt;Note&gt;&gt; allNotes;
  
  public NoteViewModel(@NonNull Application application) {
      super(application);
      repository = new NoteRepository(application);
      allNotes = repository.getAllNotes();
  }
  
  public void insert(Note note) {
      repository.insert(note);
  }
  
  public void update(Note note) {
      repository.update(note);
  }
  
  public void delete(Note note) {
      repository.delete(note);
  }
  
  public void deleteAllNotes() {
      repository.deleteAll();
  }
  
  public LiveData&lt;List&lt;Note&gt;&gt; getAllNotes() {
      return allNotes;
  }
  
  }
  </code></pre>