I keep getting a NullReferenceException was unhandled error when trying to run this code and dont know why. I am trying to sort by a persons name I have only been working with c# for about 4 Mths.
public static student[] players = new student[30];
public struct student
{
public string lastname;
public string firstname;
public string likes;
}
public static void read()
{
StreamReader sr = new StreamReader(@"names.txt");
int i = 0;
while (!sr.EndOfStream)
{
players[i].lastname = sr.ReadLine();
players[i].firstname = sr.ReadLine();
players[i].likes = sr.ReadLine();
i++;
}
sr.Close();
}
public static void sort()
{
//alphabetically lists players
student temp;
for (int i = 0; i < players.Length - 1; i++)
{
for (int pos = 0; pos < players.Length - 1; pos++)
{
if (players[pos + 1].lastname.CompareTo(players[pos].lastname) < +0)
{
temp = players[pos + 1];
players[pos + 1] = players[pos];
players[pos] = temp;
}
}
}
}