StreamReader and Writer error

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I have a problem with a program I am creating.....there are two areas where im getting a "object not set to an instance of an object" but I check to make sure the object is created before I try to do anything with it....I check to see if a file exists before I try to write to it or read from it.... see code below....

this is where I try to save a text file....

 if (File.Exists(Application.StartupPath + "\\HistData\\history.txt"))
            {
                writer = new StreamWriter(Application.StartupPath + "\\HistData\\history.txt", true);


                foreach (string item in history)
                {
                    try
                    {
                        writer.WriteLine(item);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error Saving Data " + ex.Message, "Write ERROR",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }


                }
                writer.Close();
                Environment.Exit(0);
            }
            else
            {
               
              Environment.Exit(0);
            }


this is where I try to read from the text file in the form load event.....

            string histText = Application.StartupPath + "\\HistData\\history.txt";            FileInfo historyTXT = new FileInfo(Application.StartupPath + "\\HistData\\history.txt");
            if (!File.Exists(histText))
            {
                File.Create(Application.StartupPath + "\\HistData\\history.txt");
                // MessageBox.Show("No File!");
            }
            else
            {
                reader = new StreamReader(Application.StartupPath + "\\HistData\\history.txt");
                string line; ;
                try
                {
                    while ((line = reader.ReadLine()) != null)
                    {




                        if (!compare.Contains(line))
                        {
                            compare.Add(line);
                            this.toolStripComboBox1.Items.Add(line);
                        }




                        // MessageBox.Show(line.ToString());
                    }


                }
                catch (Exception ex)
                {
                    MessageBox.Show("|||||  ERROR |||||\r\n" + ex.Message, "ERROR READING HISTORY",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


            }
            reader.Close();


not sure why this doesnt work...well..it works on the machine where I have Visual Studio installed..but I also have another machine that I test with....this machine doesnt have any files needed to run the application..so I create a setup program ...then send it to the test machine....if I comment out the streamreader code ...I dont get any errors..........?

thank you for any help....
-InkedGFX
 
It would be nice if you could specify exactly where the exception is thrown. Anyway, I'm guessing that your use of File.Create is an issue. As you have no doubt read the documentation for that method you will know that it returns an open FileStream. You don't use that FileStream so you never close it, which may well cause issues later on.
 
yes..you are absolutley correct....that was the issue....I changed the code around a little bit..I now use the "using" statement with the streamreader and writer....this fixxed the issue.....
thank you for your help

-InkedGFX
 
Back
Top Bottom