-2

I am trying to work on radio groups. I have an ok button which when clicked selects one item from four inflated radio button groups. My problem is that, when the user does not select an option from one of the groups, I have a null pointer exception.

Here is my selection code

private void getSelectedValuesAdults()
{
    rl=(RelativeLayout)findViewById(R.id.activity_selections);

    int id1 = ((RadioGroup)findViewById(R.id.KSradioGrp)).getCheckedRadioButtonId();
    int id2 = ((RadioGroup)findViewById(R.id.KTradioGrp)).getCheckedRadioButtonId();
    int id3 = ((RadioGroup)findViewById(R.id.KDradioGrp)).getCheckedRadioButtonId();
    int id4 = ((RadioGroup)findViewById(R.id.KSoradioGrp)).getCheckedRadioButtonId();

    stringChoice[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getName():generalKid[1].getName();      
    stringChoice[1]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getName():generalKid[3].getName();
    stringChoice[2]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getName():generalKid[5].getName();
    stringChoice[3]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getName():generalKid[7].getName();

    genCost[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getCost()+5:generalKid[1].getCost()+5;
    genCost[0]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getCost()+5:generalKid[3].getCost()+5;
    genCost[0]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getCost()+5:generalKid[5].getCost()+5;
    genCost[0]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getCost()+5:generalKid[7].getCost()+5;

}

The Ok button jumps to the method above. I have used if(id1==-1||id2==-1||id3==-1||id4==-1) callDialogMessage() just before the assignment to stringChoice[0] where callDialogMessage() is a message that lets you know you have not selected from a radio group but my app still crashes. What can I do? Any help will be highly appreciated

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
MarionaGlenni
  • 63
  • 1
  • 7
  • Please [edit] your question to provide a [mcve] that demonstrates the issue, and the [complete stack trace from the crash](https://stackoverflow.com/questions/23353173). – Mike M. Jan 19 '19 at 23:22
  • before storing selected values in id1,id2,id3,id4 check if(radioGroup.getCheckedRadioButtonId() == -1) – vishva kapadia Jan 21 '19 at 03:59

1 Answers1

0

On click of OK button get selected radio button Id using radio group .You can refer the below code.

     int selectedId =radioButtonGroup.getCheckedRadioButtonId();;

        // find the radiobutton by returned id
     RadioButton radioButton = FindViewById<RadioButton>(selectedId);

     if(radioButton !=null)
      {
        //do things here
      }
ramya br
  • 225
  • 2
  • 17