-2

"I am getting this error please help - at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setText(java.lang.CharSequence)' on a null object reference

package com.codewithdevesh.cricketapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.android.material.textfield.TextInputLayout;

import java.util.HashMap;

public class TeamActivity extends AppCompatActivity {
    private TextInputLayout team1,team2,overs;
    private RadioGroup radioGroup1,radioGroup2;
    private RadioButton rb_toss_t1,rb_toss_t2,rb_opted,rb_tossed;
    private Button begin;
    String toss_team,opted_team;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        team1 = findViewById(R.id.tv_team1);
        team2 = findViewById(R.id.tv_team2);

        rb_toss_t1.setText(team1.getEditText().getText().toString());
        rb_toss_t2.setText(team2.getEditText().getText().toString());

        radioGroup1 = findViewById(R.id.radio_toss);
        radioGroup2 = findViewById(R.id.radio_opted);

        rb_toss_t1 = findViewById(R.id.rb_toss_t1);
        rb_toss_t2 = findViewById(R.id.rb_toss_t2);

        overs= findViewById(R.id.tv_overs);

        int id_toss = radioGroup1.getCheckedRadioButtonId();
        int id_opted = radioGroup2.getCheckedRadioButtonId();

        rb_tossed =radioGroup1.findViewById(id_toss);
        rb_opted = radioGroup2.findViewById(id_opted);

        toss_team = rb_tossed.getText().toString();
        opted_team = rb_opted.getText().toString();

        begin = findViewById(R.id.bttn_begin);



        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(TeamActivity.this,SelectPlayersActivity.class);
                i.putExtra("opted_t",opted_team);
                i.putExtra("toss_t",toss_team);
                i.putExtra("team1",team1.getEditText().getText().toString());
                i.putExtra("team2",team2.getEditText().getText().toString());
                startActivity(i);
                finish();
            }
        });
    }
}
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Nov 22 '21 at 11:44

1 Answers1

0

init the rb_toss_t1 and rb_toss_t2 before using them And init rb_opted,rb_tossed with findViewById too

   //no change
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_team);
    team1 = findViewById(R.id.tv_team1);
    team2 = findViewById(R.id.tv_team2);
    radioGroup1 = findViewById(R.id.radio_toss);
    radioGroup2 = findViewById(R.id.radio_opted);
    // findViewById first  before using them 
    rb_toss_t1 = findViewById(R.id.rb_toss_t1);
    rb_toss_t2 = findViewById(R.id.rb_toss_t2);
    //rb_opted,rb_tossed  init
    rb_opted = findViewById(R.id.rb_opted);
    rb_tossed  = findViewById(R.id.rb_tossed  );
    //then use function setText
    rb_toss_t1.setText(team1.getEditText().getText().toString());
    rb_toss_t2.setText(team2.getEditText().getText().toString());
Matrix
  • 503
  • 3
  • 17