Domanda

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;
    }
}
È stato utile?

Soluzione

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

Altri suggerimenti

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.

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;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top