Object not set to instance of an Object ERROR!

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am receiving an "Object not set to instance of an Object" error when running this code

  {
            // read the text file
         
                if (File.Exists(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]"))
                {
                   
                    using (StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]", false))
                    {
                        string strLine = "";
                        // fill the list<string> with the contents of the text file
                        // LVResults.Items.Clear();
                        while (strLine != null)
                        {
                            char delimeter = (',');
                            strLine = sr.ReadLine();
                            string[] data = strLine.Split(delimeter);
                            if (strLine != null)
                            {
                                ListViewItem lvi = new ListViewItem();
                                lvi.Text = data[0];
                                lvi.SubItems.Add(data[1]);
                                lvi.SubItems.Add(data[2]);
                                lvi.SubItems.Add(data[3]);
                                lvi.SubItems.Add(data[4]);
                                lvi.SubItems.Add(data[5]);
                                LVResults.Items.Add(lvi);
                                LVResults.EndUpdate();
                            }
                        }
                    }
                }
                else
                    MessageBox.Show("No Data to Load!");
       
         }


im pretty sure this error is from this line - string strLine = "";
but I need this line there.......

any idea why this is?

InkedGFX
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
895
Location
Norway
Programming Experience
10+
This code will require you to read a null before loop stops:
while (strLine != null)
which means that your strLine.Split must fail since strLine is null.

change to:
while (!sr.EndOfStream)
 
Top Bottom