0

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;
    }
}
  • 1
    Welcome to Stack Overflow. Please read http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Soner Gönül Apr 27 '14 at 16:30

3 Answers3

1

Just because you've declared an array of QuestionUnit's, doesn't mean you've initialized its elements

while (line != null)
{
    var q = new QuestionUnit();
    q.Question = line;

    line = thereader.ReadLine();
    q.Answer = line;

    line = thereader.ReadLine();
    q.CorrectAnswer = line;

    line = thereader.ReadLine();
    q.Explanation = line;
    m_Questions[counter] = q;

Note: you may be better off using File.ReadAllLines and then looping through its elements with a for loop that increments by 4

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    Thank you! It didn't occur to me that each element of my array needed to be also initialized for each time a new question was read in by my file. – cennamorato Apr 27 '14 at 16:51
0

Before you can set your Question, Answer, etc, you need to set the element to an instance of an object.

//Create the object
QuestionUnit question = new QuestionUnit(...);

//Read properties
question.Question = line;
question.Answer = thereader.ReadLine();
question.CorrentAnswer = thereader.ReadLine();
question.Explanation = thereader.ReadLine();

//Set the object to an element in the array
m_Questions[counter] = question;

Without creating a new QuestionUnit, you cannot set it's properties.

Cyral
  • 13,999
  • 6
  • 50
  • 90
-1

why not you are using List<T> instead of Arrays. C# List

class QuestionBank
{
    private List<QuestionUnit> m_Questions = new List<QuestionUnit>();

    public bool ReadQuestionFile()
    {
        bool success = true;
        FileInfo theSourceFile = new FileInfo("Questions.txt");
        string line;

        try
        {
            StreamReader thereader = theSourceFile.OpenText();
            line = thereader.ReadLine();

            while (line != null)
            {
                QuestionUnit ques = new QuestionUnit();
                ques.Question = line;

                line = thereader.ReadLine();
                ques.Answer = line;

                line = thereader.ReadLine();
                ques.CorrectAnswer = line;

                line = thereader.ReadLine();
                ques.Explanation = line;

                line = thereader.ReadLine();

                m_Questions.Add(ques);
            }
        }

        catch
        {
            success = false;
        }

        return success;
    }
}
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
  • Thank you for your response. I guess a list would definitely work here as well if I didn't want to use an array. I noticed that you also solved my question by adding the "QuestionUnit ques = new QuestionUnit();" line, which was why my code was referencing a null value. – cennamorato Apr 27 '14 at 16:54
  • Why down vote ?? When ever you down vote a message pop appears that says comment below if you downvote. – Muhammad Umar Apr 27 '14 at 17:03