0

I could get the Text if I disregard the other radio group, but i need 2 columns of radio group, therefore i created another one. Since then, i couldn't get the text from both radio groups. Here is the code:

Save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try {
            Connection con = connectionClass.CONN();
            int selectedId = RadG.getCheckedRadioButtonId();
            int selectedId1 = RadG1.getCheckedRadioButtonId();
            RadioButton rb = (RadioButton) findViewById(selectedId);
            RadioButton rb1 = (RadioButton) findViewById(selectedId1);
            String Radbtn = rb.getText().toString().trim();
            String Radbtn1 = rb1.getText().toString().trim();
            String Vol = Vio.getText().toString();
            getUID = UID.getText().toString();
            String Contact = Cnumber.getText().toString();
            if (rb1.isChecked()) {
                String query = "Update Driver set ContactNumber='" + Contact + "', Violation='" + Radbtn + "',Comments='" + Vol + "' WHERE UID='" + getUID + "'";
                PreparedStatement stmt = con.prepareStatement(query);
                stmt.executeUpdate();
                Toast.makeText(Main2Activity.this, "Saved", Toast.LENGTH_SHORT).show();
            }
            if (rb1.isChecked()) {
                String query = "Update Driver set ContactNumber='" + Contact + "', Violation='" + Radbtn1 + "',Comments='" + Vol + "' WHERE UID='" + getUID + "'";
                PreparedStatement stmt = con.prepareStatement(query);
                stmt.executeUpdate();
                Toast.makeText(Main2Activity.this, "Saved", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
});

Here is the logcat:

04-12 22:19:43.904 25667-25667/com.orig3.thesis W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err:     at com.orig3.thesis.Main2Activity$5.onClick(Main2Activity.java:251)
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err:     at android.view.View.performClick(View.java:6291)
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err:     at android.view.View$PerformClick.run(View.java:24931)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:101)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at android.os.Looper.loop(Looper.java:166)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7390)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
04-12 22:19:46.349 25667-25674/com.orig3.thesis I/zygote64: Debugger is no longer active

Here is the code for my radiogroup, and I am already using OnChangelistener, I cant seem to find why it still return null for my getCheckedRadioButtonId

RadG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    RadG1.clearCheck();
                }
                isChecking = true;
            }
        });

        RadG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != -1 && isChecking) {
                    isChecking = false;
                    RadG.clearCheck();
                }
                isChecking = true;
            }
        });
  • Possible duplicate of [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) – vincrichaud Apr 12 '18 at 14:32

1 Answers1

0

You have a NullPointerException thrown on one of this two lines :

String Radbtn = rb.getText().toString().trim();
String Radbtn1 = rb1.getText().toString().trim();

It mean that one of your radio button is null, either rb or rb1. They are initialize by these lines :

RadioButton rb = (RadioButton) findViewById(selectedId);
RadioButton rb1 = (RadioButton) findViewById(selectedId1);

One being null means that findViewById() failed to find a view having selectedId as ID. One of your selected ID is not a valid value. You obtain them from these lines :

int selectedId = RadG.getCheckedRadioButtonId();
int selectedId1 = RadG1.getCheckedRadioButtonId();

If their nto valid it means getCheckRadioButtonId() returns something wrong. It means there is no radioButton selected in this group.

(you could have found this diagnostic by yourself if you had debugged your code)

Solution (Edited)

You have to check if something is selected in each of your radioGroup because you could click on the save button when nothing in selected. Has explained is the doc, if nothing is selected getCheckedRadioButtonId will return -1. So add a if selectId!=-1 To make sure something is selected.

vincrichaud
  • 2,218
  • 17
  • 34