Here is my code:-
package com.example.covid19;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
public class DetailActivity extends AppCompatActivity {
private int positonCountry;
TextView tvCountry,tvCases,tvRecovered,tvCritical,tvActive,tvTodayCases,tvTotalDeaths,tvTodayDeaths;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent= getIntent();
positonCountry=intent.getIntExtra("position",0);
getSupportActionBar().setTitle("Details of "+AffectedCountires.countryModelList.get(positonCountry).getCountry() );
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
tvCases=findViewById(R.id.tvtotalCases);
tvActive=findViewById(R.id.tvActive);
tvCountry=findViewById(R.id.tvCountry);
tvCritical=findViewById(R.id.tvCritical);
tvRecovered=findViewById(R.id.tvRecovered);
tvTodayCases=findViewById(R.id.tvtodayCases);
tvTodayDeaths=findViewById(R.id.tvTodayDeath);
tvTotalDeaths=findViewById(R.id.tvtotalDeaths);
tvCountry.setText(AffectedCountires.countryModelList.get(positonCountry).getCountry());
tvCases.setText(AffectedCountires.countryModelList.get(positonCountry).getCases());
tvActive.setText(AffectedCountires.countryModelList.get(positonCountry).getActive());
tvCritical.setText(AffectedCountires.countryModelList.get(positonCountry).getCritical());
tvRecovered.setText(AffectedCountires.countryModelList.get(positonCountry).getRecovered());
tvTodayDeaths.setText(AffectedCountires.countryModelList.get(positonCountry).getTodaysDeaths());
tvTodayCases.setText(AffectedCountires.countryModelList.get(positonCountry).getTodayCases());
tvTotalDeaths.setText(AffectedCountires.countryModelList.get(positonCountry).getDeaths());
enter code here
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId()==android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
Cause of the error:- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.covid19.DetailActivity.onCreate(DetailActivity.java:41)
The Error is in line which is in Bold