0

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.

2
Contributors
9
Replies
11
Views
6 Years
Discussion Span
Last Post by jay_el_em
0

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.

0

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

0

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?

Edited by Momerath: n/a

0

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

0

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

0

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

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.