尝试运行此代码时,我不断收到 NullReferenceException is unhandled 错误,但不知道为什么。我正在尝试按我只使用 c# 大约 4 个月的人名进行排序。
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;
}
}
}
}