i'm trying to get the radiobutton text from the radiogroup but issue is: it didn't get the value. i searched on other links but didn't helpful for me.
private EditText FirstName;
private EditText LastName;
private RadioGroup Gender;
private EditText MobileNo;
private RadioButton btn_gender;
private Button BtnNext;
private ProgressBar progressBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender=(RadioGroup) findViewById(R.id.radio_group);
MobileNo=(EditText)findViewById(R.id.btn_mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
int selectedId=Gender.getCheckedRadioButtonId();
btn_gender=(RadioButton)findViewById(selectedId);
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname=LastName.getText().toString().trim();
String gender=btn_gender.getText().toString().trim();
String phoneno=BtnNext.getText().toString().trim();
registration_form reg=new registration_form();
reg.setFirstName(fname);
reg.setLastName(lname);
reg.setGender(gender);
reg.setmobileNo(phoneno);
ref.child("registration_form").setValue(reg);
Toast.makeText(getApplicationContext(),"Registration Error", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
and i'm getting this error:
FATAL EXCEPTION: main Process: associate.aclass.associate1, PID: 28847 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference at associate.aclass.associate1.registration_form_handler$2.onClick(registration_form_handler.java:81) at android.view.View.performClick(View.java:4856) at android.view.View$PerformClick.run(View.java:19956) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
btn_gender is null you need to do this outside of Gender.setOnCheckedChangeListener to initialize btn_gender