Q:

ViewModel class throwing a null pointer when trying to display results from repository

I am trying to display results from the api once someone clicks on the adapter, I am however experiencing a null pointer with the view model class. This is the method that I want to display results with in my MatchDetailActivity
    private TeamEntity entity;
    MatchViewModel viewModel;
    private MatchAdapter adapter;
    private ActivityMatchDetailBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_match_detail);

        if(getSupportActionBar()!=null)
            getSupportActionBar().hide();

        binding.displayMatch.setLayoutManager(new LinearLayoutManager(this));
        binding.displayMatch.setHasFixedSize(true);
        adapter = new MatchAdapter(this, entity);

}

 private void getTeamEvents(){
        viewModel.getTeamEvents(entity.getTeamId()).observe(this, eventsResults ->{

            if(eventsResults != null){
                adapter.setList(eventsResults);
            }
        });
    }

But I keep getting a null pointer here:
 viewModel.getTeamEvents(entity.getTeamId()).observe(this, eventsResults
->{

Below is my stack trace any help will be appreciated.
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sportsapp/com.sportsapp.presentation.ui.MatchDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.LiveData com.sportsapp.presentation.viewmodels.MatchViewModel.getTeamEvents(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3555)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3707)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2220)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8016)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.LiveData com.sportsapp.presentation.viewmodels.MatchViewModel.getTeamEvents(java.lang.String)' on a null object reference
        at com.sportsapp.presentation.ui.MatchDetailActivity.getTeamEvents(MatchDetailActivity.java:70)
        at com.sportsapp.presentation.ui.MatchDetailActivity.onCreate(MatchDetailActivity.java:54)

A:

You have not assigned value to your view model anywhere add the below line in onCreate()
viewModel = new ViewModelProvider(this).get(MyViewModel.class);

Hope this helps

