-1

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

  • Your one of textView's Id is not matched with actual id given in layout. So at the time of setText textview is null and throwing nullPointerException – Sumit Jain Jun 07 '20 at 03:03

1 Answers1

0

In your case,when you set text to TextView, there are less possibility are text may be null. To prevent this, you have 2 solutions.

  1. Check value is null or not before set text to textView
  2. This case for you, You can write in this way,

textView.setText(value+"");