title: Unable to open activity: cannot create instance of Android View Model
body: <p>I am creating an app to scan a barcode and get that information into a Room Database using Android View Model and LiveData.
  Here is the error message</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davin.scoutingapp2019/com.example.davin.scoutingapp2019.DataDisplay}: java.lang.RuntimeException: Cannot create an instance of class com.example.davin.scoutingapp2019.TeamViewModel
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6123)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
   Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.davin.scoutingapp2019.TeamViewModel
      at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:207)
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
      at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30)
      at android.app.Activity.performCreate(Activity.java:6672)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6123) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.newInstance0(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
      at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
      at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30) 
      at android.app.Activity.performCreate(Activity.java:6672) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6123) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.davin.scoutingapp2019.TeamDAO com.example.davin.scoutingapp2019.TeamsDatabase.teamDAO()' on a null object reference
      at com.example.davin.scoutingapp2019.TeamRepository.&lt;init&gt;(TeamRepository.java:15)
      at com.example.davin.scoutingapp2019.TeamViewModel.&lt;init&gt;(TeamViewModel.java:15)
      at java.lang.reflect.Constructor.newInstance0(Native Method) 
      at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
      at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199) 
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
      at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
      at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30) 
      at android.app.Activity.performCreate(Activity.java:6672) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6123) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
  </code></pre>
  
  <p>This is the AndroidViewModel class</p>
  
  <pre><code>package com.example.davin.scoutingapp2019;
  
  import android.app.Application;
  import android.arch.lifecycle.AndroidViewModel;
  import android.arch.lifecycle.LiveData;
  
  import java.util.List;
  
  public class TeamViewModel extends AndroidViewModel {
      private TeamRepository mRepository;
      private LiveData&lt;List&lt;Team&gt;&gt; mAllData;
  
      public TeamViewModel(Application application) {
          super(application);
          mRepository = new TeamRepository(application);
          mAllData = mRepository.getAllWords();
      }
  
  
      public LiveData&lt;List&lt;Team&gt;&gt; getAllTeams() {
          return mAllData;
      }
  
      public void insert(Team team) {
          mRepository.insert(team);
      }
  
  
  }
  </code></pre>
  
  <p>Here is the class/activity DataDisplay which has an instance of TeamViewModel</p>
  
  <pre><code>package com.example.davin.scoutingapp2019;
  
  import android.arch.lifecycle.Observer;
  import android.arch.lifecycle.ViewModelProviders;
  import android.content.Intent;
  import android.support.annotation.Nullable;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.widget.Toast;
  
  import java.util.List;
  
  public class DataDisplay extends AppCompatActivity {
  
  
      private TeamViewModel teamViewModel;
      public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_data_display);
          RecyclerView recyclerView = findViewById(R.id.recyclerview);
          final TeamListAdapter adapter = new TeamListAdapter(this);
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          teamViewModel = ViewModelProviders.of(this).get(TeamViewModel.class);
          teamViewModel.getAllTeams().observe(this, new Observer&lt;List&lt;Team&gt;&gt;() {
              @Override
              public void onChanged(@Nullable final List&lt;Team&gt; teams) {
                  // Update the cached copy of the words in the adapter.
                  adapter.setTeams(teams);
              }
          });
      }
  
  
  
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE &amp;&amp; resultCode == RESULT_OK) {
              Destring destring=new Destring(data.getStringExtra("str"));
              Team newTeam= new Team(destring.getTeamNumber(),destring.getPosition(),destring.getSandstorm("hab line"),destring.getSandstorm("cargo balls"),destring.getSandstorm("cargo hatches"),destring.getSandstorm("rocket balls"),destring.getSandstorm("rocket hatches"),destring.getTeleop("cargo balls"),destring.getTeleop("cargo hatches"),destring.getTeleop("rocket balls"),destring.getTeleop("rocket hatches"),destring.getRocketRole(),destring.getClimberRole(),destring.getOverallRole(),destring.getOtherComments());
  
              teamViewModel.insert(newTeam);
          } else {
              Toast.makeText(
                      getApplicationContext(),
                      R.string.empty_not_saved,
                      Toast.LENGTH_LONG).show();
          }
      }
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>This question is similar to another <a href="https://stackoverflow.com/questions/48605672/cannot-create-an-instance-of-viewmodel-class-unable-to-start-activity-component?rq=1">question</a> but I only have one parameter in my constructor for TeamViewModel.</p>
  
  <p>The rest of the code is on <a href="https://github.com/sejudav20/A-Spartan-Scouting-App-" rel="nofollow noreferrer">github</a></p>
  
  <p>Thank You</p>