1

In this program I'm just trying to sort some data in a SortedSet. My error occurs in the CompareTo-method in the Person Class, at this line:

ret = Birthday.CompareTo(p.Birthday);

That line throws a System.NullReferenceException because "object instance is not set to an instance of an object"

Can somebody please tell me where my bug is?

Main Class:

using System;
using System.Collections.Generic;

namespace CollectionsPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<Person> sortedSet = new SortedSet<Person>();
            Person[] people = new Person[] 
            {
                new Person("Hans", "Gruber", new Date(1, 6, 2000)),
                new Person("Werner", "Müller", new Date(1, 9, 2000)),
                new Person("Alois", "Gringer", new Date(1, 1, 2010)),
                new Person("Günther", "Gruber", new Date(7, 10, 2003)),
                new Person("Michael", "Leberwurst", new Date(9, 11, 2000)),
                new Person("Thomas", "Unge", new Date(1, 12, 2004)),
                new Person("Elias", "Hunger", new Date(11, 12, 2001)),
                new Person("Yoda", "Jedi", new Date(21, 2, 1880)),
                new Person("Yoda", "Jedi", new Date(21, 2, 1881))
            };

            Console.WriteLine("Unsorted:");
            foreach(Person p in people) 
            {
                sortedSet.Add(p);
                Console.WriteLine("{0}", p);
            }

            Console.WriteLine("Sorted:");
            foreach(Person p in sortedSet)
            {
                Console.WriteLine(p);   
            }


            Console.ReadKey();
        }
    }
}

Person Class (the data I want to sort):

using System;

namespace CollectionsPractice
{
    public class Person : IComparable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Date Birthday { get; set; }

        public Person(string Vorname, string Nachname, Date Geburtsdatum) 
        {
            this.FirstName = Vorname;
            this.LastName = Nachname;
            this.Birthday = Birthday;
        }

        public override string ToString()
        {
            return string.Format("[Person: FirstName={0}, LastName={1}, Birthday={2}]", FirstName, LastName, Birthday);
        }

        public int CompareTo(object obj)
        {
            if ((obj == null) || !(GetType().Equals(obj.GetType())))
                return -1;
            else
            {
                Person p = obj as Person;
                int ret = FirstName.CompareTo(p.FirstName);
                if (ret == 0) 
                {
                    ret = LastName.CompareTo(p.LastName);
                    if (ret == 0) 
                    {
                        ret = Birthday.CompareTo(p.Birthday);
                    }
                }
                return ret;
            }
        }
    }
}

Date Class:

using System;
namespace CollectionsPractice
{
    public class Date : IComparable
    {
        public int Day { get; set; }
        public int Month { get; set; }
        public int Year { get; set; }

        public Date (int Day, int Month, int Year) 
        {
            this.Day = Day;
            this.Month = Month;
            this.Year = Year;
        }

        public override string ToString()
        {
            return string.Format("{0}.{1}.{2}", Day, Month, Year);
        }

        public int CompareTo(object obj)
        {
            if ((obj == null) || !(GetType().Equals(obj.GetType())))
                return -1;
            else
            {
                Date d = obj as Date;
                int ret = Day - d.Day;
                if (ret == 0)
                {
                    ret = Month - d.Month;
                    if (ret == 0)
                    {
                        ret = Year - d.Year;
                    }
                }
                return ret;
            }
        }
    }
}
Stefan
  • 546
  • 1
  • 3
  • 22
  • I recommend you read the duplicate, there are many helpful debugging tips in there that will help you solve your problem. I also recommend you learn how to use your IDE so you can break when an Exception is thrown and then learn how to inspect the state of the application (local variables, watches, etc). – Igor Mar 28 '18 at 18:41

0 Answers0