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;
}
});