-3

Code:

namespace DriversLicenceExam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            LoadKey();
            LoadAnswer();
        }
        private void LoadKey()
        {
            try
            {
            const int AnsLgth = 20;
            string[] Answers = new string[AnsLgth];

            int index = 0;

            System.IO.StreamReader inputFile;

            inputFile = File.OpenText("DRIVERKEY.txt");

            {
                Answers[index] = (inputFile.ReadLine());
                index++;
            }

            inputFile.Close();


            for (int i = 0; i <= Answers.Length; i++)
            {
                string value = Answers[i];
                listBox1.Items.Add(value);
            }

            }
            catch (Exception)
            {

                throw;
            }
        }

I keep getting an error stating 'Value cannot be null. Parameter name; item'

I am relatively new to coding and not sure what this means. Any help or input is appreciated.

The goal of this program is to insert an answer key file, turn it into an array, output it into a listbox, then do the same with answer files, grade the answer files that are submitted by "students" by crossreferencing them with the answer key array, then output the incorrect answers into another listbox.

This is only the first method where I am supposed to input the answer key file then turn it into an array and display it in a listbox.

The answer key text file looks like this:

B
D
A
A
C
A
B
A
C
D
B
C
D
A
D
C
C
B
D
A
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    Which operation throws the exception? When you debug, what are the runtime values used in that operation? – David Nov 02 '22 at 18:23
  • There are linebreaks between the answers in your file? Your code seem to expects that but your example at the end does not show any. – Ralf Nov 02 '22 at 18:29
  • 1
    After debugging your code and seeing the problem yourself you may find using File.ReadAllLines helpfull to fix it. – Ralf Nov 02 '22 at 18:33
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mark Benningfield Nov 02 '22 at 18:37

2 Answers2

2

You're reading one line from the file into the first element of the array. So the rest of the elements are null. Then you're trying to add all of the elements to a UI control:

listBox1.Items.Add(value);

It's accepting the first non-null value but the remaining 19 null values are problematic.

I suspect you meant for this to be a loop:

{
    Answers[index] = (inputFile.ReadLine());
    index++;
}

But you forgot... the loop. Perhaps something like this:

var line = "";
while ((line = inputFile.ReadLine()) != null)
{
    Answers[index] = line;
    index++;
}

Though even then, there's really no guarantee that the number of lines will always equal the hard-coded array size. If there are fewer, the same error will occur. If there are more, a different error will occur.

This is an example where a more dynamic data structure, such as a List<>, would be helpful. Something like this:

var Answers = new List<string>();

Then the collection will automatically grow to only the size needed as you add to it. So the loop would be:

var line = "";
while ((line = inputFile.ReadLine()) != null)
{
    Answers.Add(line);
}

Using a List<> is slightly different from using an array. For example, you'd use .Count instead of .Length. But overall what you have is conceptually the same. A List<> is just more dynamic than a fixed-length array.

David
  • 208,112
  • 36
  • 198
  • 279
1

Here

inputFile = File.OpenText("DRIVERKEY.txt");

{
    Answers[index] = (inputFile.ReadLine());
    index++;
}

There is no loop around the part that reads a line from the file and assigns it to the array. So only one line is read and only the first entry in the array is filled.

There are further problems further down in the code. But try to understand and fix that one first. Tip: Learn how to work with the debugger. Set breakpoints, step through the code line by line and look at the variables while the code executes.

Here is a good video tutorial on using the debugger in Visual Studio: https://www.youtube.com/watch?v=sACkw915kmg

NineBerry
  • 26,306
  • 3
  • 62
  • 93