I have a simple array, where I store the 5 best times for a game. I then write the array elements to a text file . I get the message that a string reference is not set to an instance of a string

private void Scores()
        {
            if (!File.Exists("TopTimes1.txt"))
            {
                File.Create("TopTimes1.txt");
                StreamWriter sw = new StreamWriter("TopTimes1.txt");
                for (int i = 1; i < 6; i++) 
                {
                    [B][I]sw.WriteLine(TimeSpan.Parse(i.ToString()));[/I][/B]
                }
                sw.Close();
            }
            else
            {
                StreamReader sr = new StreamReader("TopTimes1.txt");
                for (int i = 0; i < 5; i++)
                {
                    bestTimes[i] = TimeSpan.Parse(sr.ReadLine());
                }
                sr.Close();
            }
            
        }

        private void HighestScore()
        {
            
            Array.Sort(bestTimes);
            if ( timerTime < bestTimes[4])
            {
                bestTimes[4] = timerTime;
                Array.Sort(bestTimes);
                StreamWriter sw = new StreamWriter("TopTimes.txt");
                for (int i=0; i<5; i++)
                {
                    sw.WriteLine(bestTimes[i]);
                }
            } 
        }

I get the message in the bold italics line.

Big blunder!!
Error is not that line, let me try that again:

TimeSpan[] bestTimes = new TimeSpan[5];

        private void Scores()
        {
            if (!File.Exists("TopTimes1.txt"))
            {
                File.Create("TopTimes1.txt");
                StreamWriter sw = new StreamWriter("TopTimes1.txt");
                for (int i = 1; i < 6; i++) 
                {
                    sw.WriteLine(TimeSpan.Parse(i.ToString()));
                }
                sw.Close();
            }
            else
            {
                StreamReader sr = new StreamReader("TopTimes1.txt");
                for (int i = 0; i < 5; i++)
                {
                    [B][I]bestTimes[i] = TimeSpan.Parse(sr.ReadLine());[/I][/B]
                }
                sr.Close();
            }
            
        }

        private void HighestScore()
        {
            
            Array.Sort(bestTimes);
            if ( timerTime < bestTimes[4])
            {
                bestTimes[4] = timerTime;
                Array.Sort(bestTimes);
                StreamWriter sw = new StreamWriter("TopTimes.txt");
                for (int i=0; i<5; i++)
                {
                    sw.WriteLine(bestTimes[i]);
                }
            } 
        }

This time I got it right. Error is with the bold italics line.

Since you changed the error, I'm changing my post. Hold on :)

sorry about that, I'm at work, and have to do all this on the sly

First thing

File.Create("TopTimes1.txt");
                StreamWriter sw = new StreamWriter("TopTimes1.txt");

There is no need for the first line, StreamWriter will create the file if it doesn't exist.

Second, have you opened the file with notepad (or whatever text editor makes you happy) to see what is in the file? Are there 5 lines?

have just done that before you replied, the file is created, but nothing is written to it

Try calling sw.Flush() before you close the stream. Close is supposed to flush the file but sometimes it doesn't.

I did actually get a message once or twice that the file was in use. That might be the problem

think I'm giving up on the text file idea, too many strange things happening. Will start over and try and come up with a better idea of how to keep track of the high scores

sat down and started fresh this morning, everything working as it should. My problem was just the casting between timespan and string. Got it right, and will post my new work as soon as I have web connectivity on my pc again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.