使用C#中的CompareTo在我的源代码中出错

时间:2018-03-28 18:37:38

标签: c# nullreferenceexception compareto

在这个程序中,我只想尝试对SortedSet中的一些数据进行排序。我的错误发生在Person Class的CompareTo方法中,在这一行:

ret = Birthday.CompareTo(p.Birthday);

该行抛出System.NullReferenceException,因为"对象实例未设置为对象的实例"

有人可以告诉我我的错误在哪里吗?

主类:

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(我要排序的数据):

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

日期类:

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

0 个答案:

没有答案