i am developing a simple MCQ quiz,the code below is supposed to show if the user response was correct or wrong and highlight the correct answer if the user selected the wrong one. It works perfectly fine for the first question, if i press next to move on to the next question i get this
03-08 18:47:12.701 3230-3230/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.useradmin.dbquiz, PID: 3230 java.lang.NullPointerException at com.example.useradmin.dbquiz.MainActivity$1.onCheckedChanged(MainActivity.java:109) at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174) at android.widget.RadioGroup.access$600(RadioGroup.java:54) at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358) at android.widget.CompoundButton.setChecked(CompoundButton.java:130) at android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:181) at android.widget.RadioGroup.check(RadioGroup.java:161) at android.widget.RadioGroup.clearCheck(RadioGroup.java:209) at com.example.useradmin.dbquiz.MainActivity$2.onClick(MainActivity.java:125) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
here is my code:
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
rda = (RadioButton) findViewById(R.id.rdb1);
rdb = (RadioButton) findViewById(R.id.rdb2);
rdc = (RadioButton) findViewById(R.id.rdb3);
rdd = (RadioButton) findViewById(R.id.rdb4);
butNext = (Button) findViewById(R.id.btnNext);
rbgroup = (RadioGroup) findViewById(R.id.rbGroup);
mquestionsList = myDbHelper.getListQuestions();
moptionsList = myDbHelper.getListOptions();
currentQ = mquestionsList.get(question_id);
currentOptions = moptionsList.get(option_id);
setQuestion();
rbgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group.getCheckedRadioButtonId() != -1)
{
RadioButton user_response = null;
int i = 0;
while (user_response == null && i < group.getChildCount()) {
RadioButton temp = (RadioButton) group.getChildAt(i);
String answer = temp.getText().toString();
if (currentQ.getCorrect_Ans().equalsIgnoreCase(answer)) {
user_response = temp;
}
i++;
}
int checked = group.getCheckedRadioButtonId();
RadioButton checkedBtn = (RadioButton) findViewById(checked);
if (user_response == checkedBtn) {
score++;
} else {
checkedBtn.setTextColor(Color.RED);
}
user_response.setTextColor(Color.GREEN);
Toast.makeText(getApplicationContext(),""+score,Toast.LENGTH_SHORT).show();
}
}
});
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (question_id < mquestionsList.size()) {
currentQ = mquestionsList.get(question_id);
currentOptions = moptionsList.get(option_id);
rbgroup.clearCheck();
//rbgroup.setEnabled(false);
setQuestion();
}
}
});
}
private void setQuestion() {
txtQuestion.setText(currentQ.getQuestion_id() + ". " + currentQ.getQuestion());
rda.setText(currentOptions.getOpt1());
rdb.setText(currentOptions.getOpt2());
rdc.setText(currentOptions.getOpt3());
rdd.setText(currentOptions.getOpt4());
question_id++;
option_id++;
}
error is on this line: user_response.setTextColor(Color.GREEN);