-2

every time i press register button it close the application and when i remove the radio button from the if condition and make one of the radio buttons checked from xml at activity start it works fine her is the code


 // get selected radio button from radioGroup
                  int selectedId = radioGroup.getCheckedRadioButtonId();

              // find the radiobutton by returned id
              radioButton = (RadioButton) findViewById(selectedId);

              String Gender = radioButton.getText().toString();

              if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && radioGroup.getCheckedRadioButtonId() < 0) {
                  return;
              }

              else if (fName.isEmpty() || lName.isEmpty() || eUsername.isEmpty() || ePassword.isEmpty() || radioGroup.getCheckedRadioButtonId() < 0) {
                  Snackbar.make(view, "Please enter your full data", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }


              else {
                  //********************************************************************************
                  if(Objects.equals(password.getText().toString(),RPTpassword.getText().toString())) {

i tried too many solution as initializing the two radio buttons and make condition


if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && !radioButton1.isChecked() && !radioButton2.isChecked()) {
                      return;
                  }

and the same problem and tried


if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && Gender != "Male" && Gender != "Female") {
                          return;
                      }

and nothing it close every time i press register

the error


 AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.android.loginregister, PID: 3713
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
                          at com.android.loginregister.RegisterActivity$2.onClick(RegisterActivity.java:131)
                          at android.view.View.performClick(View.java:5610)
                          at android.view.View$PerformClick.run(View.java:22265)
                          at android.os.Handler.handleCallback(Handler.java:751)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

<RadioGroup
            android:id="@+id/rdioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="25dp"
            android:layout_below="@id/RePassword">

            <RadioButton
                android:text="Male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton3"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                android:buttonTint="#FFFFFF"
                android:textSize="14sp"
                android:textColorLink="#e8d829"
                android:focusableInTouchMode="false"
                android:checked="false" />

            <RadioButton
                android:text="Female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                android:buttonTint="#FFFFFF"
                android:textSize="14sp"
                android:textColorLink="#2362e0" />

        </RadioGroup>
DEVAN.A
  • 13
  • 3
  • What is the error in logcat? – Sava Dimitrijević Sep 14 '18 at 01:39
  • [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). – ADM Sep 14 '18 at 03:39
  • I'm sorry i have attached it with the code but i Ctrl+z it Accidentally .. i attached it again – DEVAN.A Sep 14 '18 at 13:48
  • What is line 131 ? – John Joe Sep 14 '18 at 13:51
  • String Gender = radioButton.getText().toString(); .. i have made string carring the radiobutton name as male or female to send it to mysql database – DEVAN.A Sep 14 '18 at 13:53
  • your String Gender is null! – DarShan Sep 14 '18 at 13:53
  • i used Gender here >>> RegisterRequest registerRequest = new RegisterRequest(fName, lName, eUsername, ePassword, Gender, responseListener); RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this); queue.add(registerRequest); – DEVAN.A Sep 14 '18 at 13:55

1 Answers1

0

This line is wrong.

 radioButton = (RadioButton) findViewById(selectedId);

The id should be same in your xml radioButton attributes.

In your case, you should initialize two radio buttons like this

radioBtn2 = (RadioButton)findViewById(R.id.radioButton2);
radioBtn3 = (RadioButton)findViewById(R.id.radioButton3);

And since you want to get the text from two radioButtons, you have to declare two Strings.

String gender1 = radioBtn2.getText().toString();
String gender2 = radioBtn3.getText().toString();

And finallly

  if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && gender1 != "Male" && gender2 != "Female") {
     return;
   }

To compare String, we should use .equals(). For not equals, it should be

if (!gender1.equals("Male")) {
John Joe
  • 10,340
  • 9
  • 48
  • 104