0

This code here does not work and throws the exception 'object reference not set to an instance of an object'

I'm not sure why this is happening as I'm pretty sure I set the value of 'line'

public void LoadCasesDataFile(string filename)
        {
            int countryCount = 0;
            int dateCount = 0;
            using (StreamReader file = new StreamReader(filename))
            {
                while (!file.EndOfStream)
                {
                    if (countryCount < 206)
                    {
                        while (dateCount < 110)
                        {
                            var line = file.ReadLine();
                            var values = line.Split(',');

                            if (values[0] == CountriesArray[countryCount].GetName()) 
                            {
                                DateTime testDate = Convert.ToDateTime(values[1]);



                                CountriesArray[countryCount].AddDate(Convert.ToDateTime(values[1]), Convert.ToInt32(values[2]), Convert.ToInt32(values[3]), dateCount);

                                dateCount++;


                            }


                        }

                        countryCount++;
                    }
                }
            }
        }
  • 1
    The [documentation for `ReadLine`](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.readline?view=netframework-4.8) states: _"The next line from the input stream, or null if the end of the input stream is reached."_. Does the file have 110 rows? – ProgrammingLlama Apr 20 '20 at 04:46
  • Anyway, the reason `.Split(',')` is throwing this exception is because `line` is `null`. – ProgrammingLlama Apr 20 '20 at 04:48

0 Answers0