lluvatar
lluvatar

Reputation: 3

Writing to a text file in C#, but there is an exception unhandled error

I have been making a quiz for a while now, and wanted to implement a leader board. The leader board should show the name of the user, score

I have checked to see if it is recognising the file and when I remove the score it works fine. The score works fine when I am displaying it on a label in all the other forms.

public partial class frmLeaderboard : Form {enter code here //Setup player list List players = new List();

    public frmLeaderboard()
    {
         //Setup form
         InitializeComponent();
         dgLeaderboard.ColumnCount = 3;
         dgLeaderboard.Columns[0].Name = "Player Name";
         dgLeaderboard.Columns[1].Name = "Score"; 
         dgLeaderboard.Columns[2].Name = "Level";
         SaveScores();
         GetPreviousPlayers();
    }

    private void GetPreviousPlayers()
    {
        //searches for file and loads score
        if(File.Exists("previousplayers.txt"))
        {
            LoadScores();
        }

        dgLeaderboard.Sort(dgLeaderboard.Columns[1], ListSortDirection.Descending);
    }

    private void LoadScores()
    {
        if (File.Exists("previousplayers.txt"))
        {
            //Loads the score
            var playerScores = File.ReadAllLines("previousplayers.txt");

            if (playerScores.Length > 0)
            {
                //bring in the players to the grid
                foreach (var players in playerScores)
                {
                    var splitDetails = players.Split('~');
                    dgLeaderboard.Rows.Add(splitDetails[0], Convert.ToInt32(splitDetails[0]), splitDetails[2]);
                }
            }

            else
            {
                HideGrid();
            }
        }
    }

    private void SaveScores()
    {
        FileStream fileStream = new FileStream("previousplayers.txt", FileMode.Append, FileAccess.Write);
        StreamWriter streamWriter = new StreamWriter(fileStream);

        //Seperate the username, score and level
        try
        {
            foreach(var player in players)
            {
                streamWriter.WriteLine(player.Username + "~" + player.Score + "~" + player.Level);
            }
        }
        catch(Exception)
        {
            MessageBox.Show("Error Loading the scores", "Please try again");
        }
        finally
        {
            streamWriter.Close();
            fileStream.Close();
        }

    }

    private void HideGrid()
    {
        //Sets the grid to invisible
        dgLeaderboard.Visible = false;
    }



}

In the form before I use this code

string filePath = "previousplayers.txt";
        FileStream aFile;
        StreamWriter sw;

        try
        {`enter code here`
            if (!File.Exists(filePath))
            {
                aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            }
            else
            {
                aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write);
            }

            sw = new StreamWriter(aFile);
            sw.WriteLine(frmStart.Player.Username + "~" + frmStart.Player.Score + "~" + frmStart.Player.Level);
            sw.Close();
            aFile.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show("User's details have not been saved", "Error Occurred");
        }

It crashes with the error: Exception Unhandled, System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

at this line dgLeaderboard.Rows.Add(splitDetails[0], Convert.ToInt32(splitDetails[0]), splitDetails[2]);

Blockquote

Upvotes: 0

Views: 261

Answers (1)

You're writing the player scores this way:

player.Username + "~" + player.Score + player.Level

So if you're reading it and splitting it on ~, you'll get an array with a length of 2, where [0] is player.Username and [1] is player.Score + player.level. You're trying to access index [2], which is out of bounds.

Upvotes: 1

Related Questions