1

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;
                    }
                }
            }

        }
Kiwilad
  • 85
  • 1
  • 1
  • 10
  • How are you populating your players array? Are all items in the array valid objects? Please post your code where you populate 'players'. – Baldrick Nov 13 '13 at 01:51
  • I'm imagining that 'players' is a static array of 'student' declared in the class definition. – Baldrick Nov 13 '13 at 01:52
  • Have you created new instance for 'players' ? – jhyap Nov 13 '13 at 01:53
  • may be you can update your code inside your post – jhyap Nov 13 '13 at 02:00
  • NullReference is usually referring to an object is not created – jhyap Nov 13 '13 at 02:02
  • There is space for 30 students preallocated in `students` array. `names.txt` probably contains less than 90 lines. For a quick fix store last value of `i` and use that value to limit first `for loop` instead of `players.Length` which is always 30. – Nikola Markovinović Nov 13 '13 at 02:32

3 Answers3

1

I might have caught the NullReference

while (!sr.EndOfStream)
{
    player[i] = new student();
    players[i].lastname = sr.ReadLine();
    players[i].firstname = sr.ReadLine();
    players[i].likes = sr.ReadLine();
    i++;
}
jhyap
  • 3,423
  • 5
  • 24
  • 46
  • cannot apply indexing with [] to an expression – Kiwilad Nov 13 '13 at 02:15
  • this is error message you received when you tried player[i][pos]? Because initially when I see your for loops, I presumed it was a jagged array. lol.. So, just use player[pos] will do.. – jhyap Nov 13 '13 at 02:17
1

You need to check the array item is not null before you compare the lastname.

private static void Sort(Player[] players)
{
    //alphabetically lists players

    for (int i = 0; i < players.Length; i++)
    for (int pos = 0; pos < players.Length - 1; pos++)
    {
        var currentPlayer = players[pos] != null ? players[pos].LastName : null;
        var nextPlayer = players[pos + 1] != null ? players[pos + 1].LastName : null;

        if (Compare(currentPlayer, nextPlayer) > 0)
        {
            var temp = players[pos + 1];
            players[pos + 1] = players[pos];
            players[pos] = temp;
        }
    }
}

private static int Compare<T>(T current, T next)
    where T: IComparable
{
    if (current == null) return -1;
    if (next == null) return 1;
    return current.CompareTo(next);
}
Dustin Kingen
  • 19,074
  • 6
  • 47
  • 90
0

Sorting is already built into the .NET framework via Enumerable.OrderBy and EnumerableOrderByDescending. In your case you can greatly reduce the function:

public static IOrderedEnumerable<student> sort()
{
   IOrderedEnumerable<student> orderedPlayers = players.OrderBy(p => p.lastname);
   return orderedPlayers;
}

This will order the results without throwing an exception even if lastName is null.

P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327