I am implementing radio group. When i try to run the app, i get this error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference.
radio_group_your_choice = (RadioGroup) findViewById(R.id.radio_group_your_choice);
male_radioButton = (RadioButton) findViewById(R.id.male_radioButton);
female_radioButton = (RadioButton) findViewById(R.id.female_radioButton);
radio_group_your_choice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) findViewById(checkedId);
gender = radioButton.getText().toString();
}
});
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="15dp">
<RadioGroup
android:id="@+id/radio_group_your_choices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/delivery_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Up"
android:textColor="@color/dark_grey"/>
<RadioButton
android:id="@+id/pickup_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="Delivery"
android:textColor="@color/dark_grey"/>
</RadioGroup>
</RelativeLayout>