0

I am getting an error (System.NullReferenceException: 'Object reference not set to an instance of an object.') due to a null value even though I assign a value to the array entry the line prior. I have created a class that stores questions, answers and correct answers. I will have 10 entries in this array with 10 unique questions and answers, it needs to be an array to allow for me to effeciently loop through it.

namespace Quiz
{
    public class Question
    {
        public string Que { get; }
        public string Ans1 { get; }
        public string Ans2 { get; }
        public string Ans3 { get; }
        public string Ans4 { get; }
        public string CorrectAns { get; set; }

        public Question(string que, String ans1, String ans2, String ans3, String ans4, String correctans)
        {
            Que = que;
            Ans1 = ans1;
            Ans2 = ans2;
            Ans3 = ans3;
            Ans4 = ans4;
            CorrectAns = correctans;
        }
    }
    public partial class Form1 : Form
    {
        public Question[] CurrentQuestion;

        public Form1()
        {
            InitializeComponent();
            CurrentQuestion[0] = new Question("What number is lowest", "1", "2", "3", "4", "2");
            //Error Occurs on line below
            MessageBox.Show(CurrentQuestion[0].Que);           
        }

    }
}

0 Answers0