0

On Adding radioButton in correctAnswer(list of RadioButton) it show System.ArgumentNullException:'Value cannot be null'

namespace QuestionPaper
{
   public partial class Form1 : Form
   {
       RadioButton[] correctAnswers;
       int result { get; set; }
       
       public Form1()
       {
            InitializeComponent();
            getCorrectAnswers();
       }

       private void getCorrectAnswers()
       {
           correctAnswers.Append(radioButton1);
           correctAnswers.Append(radioButton5);
           correctAnswers.Append(radioButton11);
           correctAnswers.Append(radioButton14);
       }
       private void button1_Click(object sender, EventArgs e)
       { 
           foreach (RadioButton answer in correctAnswers) 
           {
              result = answer.Checked ? result + 1 : result;
           }
           label1.Text = label1.Text + " " + result.ToString();
       }       
}

}

1 Answers1

1

In your code, you are not intializing the correctAnswers variable. In your getCorrectAnsweres(), I would recommend initializing the correctAnswers first then adding data to it.

Also, use List so that you dont have to hard code the number of records it can hold.

List<RadioButton> correctAnswers = new List<RadioButton>();

for Lists, you can simply use, Add() method.

correctAnswers.Add(radioButton1); // make sure radioButton1 and others are initialized.
Jawad
  • 11,028
  • 3
  • 24
  • 37