0

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference

    final RadioGroup rg1=(RadioGroup)findViewById(R.id.Q1Group);
    final RadioGroup rg2=(RadioGroup)findViewById(R.id.Q2group);
    final RadioGroup rg3=(RadioGroup)findViewById(R.id.Q3Group);
    final RadioGroup rg4=(RadioGroup)findViewById(R.id.Q4Group);
    final RadioGroup rg5=(RadioGroup)findViewById(R.id.Q5Group);
    Button submitButton = (Button) findViewById(R.id.SubmitButton);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int score = 0;
            if (((RadioButton) findViewById(rg1.getCheckedRadioButtonId())).getText().equals(Integer.toString(q1num))) {
                score++;
            }

            if (((RadioButton) findViewById(rg2.getCheckedRadioButtonId())).getText().equals(Integer.toString(q2num))) {
                score++;
            }

            if(((RadioButton) findViewById(rg3.getCheckedRadioButtonId())).getText().equals(Integer.toString(q3num))){
                score++;
            }

            if(((RadioButton) findViewById(rg4.getCheckedRadioButtonId())).getText().equals(Integer.toString(q4num))){
                score++;
            }

            if(((RadioButton) findViewById(rg5.getCheckedRadioButtonId())).getText().equals(Integer.toString(q5num))){
                score++;
            }

            TextView scoreView = (TextView) findViewById(R.id.TotalScore);
            scoreView.setText(Integer.toString(score) + " / 5");
        }
    });
  • Are rg1, rg2, rg3, rg4, rg5 all instantiated? – Al Lelopath Jan 31 '17 at 21:39
  • Since there are already thousands of questions about NullPointerException you should explain why you think this one is different than all the others and/or why those other questions didn't help you solve your problem. – takendarkk Jan 31 '17 at 21:45
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – takendarkk Jan 31 '17 at 21:57
  • Because I'm working on project for a competition that is due in two days and I been having this problem for a couple of days if you don't mind helping me out that would be great – Anonymous International Jan 31 '17 at 21:57
  • Is not a duplicate because is a radio button – Anonymous International Jan 31 '17 at 21:58
  • 1
    The fact that it is a radio button makes 0 difference. If you read the answers on the linked question you will learn how to debug this on your own. – takendarkk Jan 31 '17 at 22:15
  • Use a debugger to find where the exception occurs. There is not enough code to guess the source of the problem. Another idea would be to use the view ids more reasonable. It seems that you are using a button group `rg1` to get the checked radio button `id`. With this id, you get the view and compare the `text` of the view to another value `q1num`. So complicated! I'd try to manage questions and ids together (for example in a dictionary) and evaluate later, wich `id` was checked. – ventiseis Jan 31 '17 at 22:57

0 Answers0