I am trying to read lines from a file and save them to an array of questions and answers. When I attempt to save one of the lines to a private member via the "set" property, I am getting an "Object reference not set to an instance of an object" error. Since my variable has been declared locally in the class as a private member, how could this be a null object? Any help would be appreciated. My debugger claims the null reference is on the line "m_Questions[counter].Question = line;". The "Questions.txt" file does have valid text in it. The debugger shows the first line coming in correctly when stepping through the code, but when it attempts to save the first line to "m_Questions[counter].Question" (my setter property), it halts.
class QuestionBank
{
private const int NUM_ANSWERS = 4;
private const int NUM_QUESTIONS = 5;
private QuestionUnit[] m_Questions = new QuestionUnit[NUM_QUESTIONS];
public bool ReadQuestionFile()
{
bool success = true;
FileInfo theSourceFile = new FileInfo("Questions.txt");
string line;
int counter = 0;
try
{
StreamReader thereader = theSourceFile.OpenText();
line = thereader.ReadLine();
while (line != null)
{
m_Questions[counter].Question = line;
line = thereader.ReadLine();
m_Questions[counter].Answer = line;
line = thereader.ReadLine();
m_Questions[counter].CorrectAnswer = line;
line = thereader.ReadLine();
m_Questions[counter].Explanation = line;
line = thereader.ReadLine();
counter++;
}
}
catch
{
success = false;
}
return success;
}
}