我想排序列表,但它不起作用

时间:2012-08-17 18:10:30

标签: c# c#-4.0 c#-3.0

我有一个产品类:

       public class Product
{
    private string firstname;
    private string lastname;
    private string email;

    public Product()
    {
    }

    public Product(string firstname, string lastname, string email)
    {
        this.Firstname = firstname;
        this.Lastname = lastname;
        this.Email = email;
    }

    public string Firstname
    {
        get
        {
            return firstname;
        }
        set
        {
            firstname = value;
        }
    }

    public string Lastname
    {
        get
        {
            return lastname;
        }
        set
        {
            lastname = value;
        }
    }

    public string Email
    {
        get
        {
            return email;
        }
        set
        {
            email = value;
        }
    }

    public virtual string  GetDisplayText(string sep)
    {
        return Firstname + sep + Lastname + sep + Email;
    }

}

我再做一个班级,我正在做ICompare

       public class PersonSort : IComparer<Product>
{
    public enum CompareType
    {
        Email
    }

    private CompareType compareType;

    public PersonSort(CompareType cType)
    {
        this.compareType = cType;
    }

    public int Compare(Product x, Product y)
    {
        if (x == null) throw new ArgumentNullException("x");
        if (y == null) throw new ArgumentNullException("y");

        int result;
        switch (compareType)
        {
            case CompareType.Email:
                return x.Email.CompareTo(y.Email);
            default:
                throw new ArgumentNullException("Invalid Compare Type");
        }
    }
}

然后我调用ProductList Class

        List<Product> person;

          public void Sort()
    {
        person.Sort(new PersonSort(PersonSort.CompareType.Email));
    } 

然后这个方法调用Form:

       private ProductList products = new ProductList();

           private void button4_Click(object sender, EventArgs e)
    {
        products.Sort();
    }

但它显示null异常:对象引用未设置为对象的实例。** 你能帮帮我。怎么解决?

3 个答案:

答案 0 :(得分:3)

你在某个地方有一个null引用。确保列表已初始化。此外,Product.Email是否已正确设置?

您可能希望改用StringComparer。取代

return x.Email.CompareTo(y.Email);

return StringComparer.Ordinal.Compare(x.Email, y.Email);

答案 1 :(得分:1)

根据提供的代码,person中的ProductList未初始化。话虽如此,如果您在问题中包含异常的调用堆栈,您将获得明确的答案。

List<Product> person;

List<Product> person = new List<Product>();

答案 2 :(得分:1)

List<Product> person;

这给了一个值?您没有在代码中包含person列表并向其添加项目(或将项目添加到列表中,然后将其分配给person等)。那里的错误可能导致问题。

public int Compare(Product x, Product y)
{
    if (x == null) throw new ArgumentNullException("x");
    if (y == null) throw new ArgumentNullException("y");

这是个坏主意,因为它是IComparer<T>.Compare文档的一部分,传入null是可以的,并且null参数的评估小于任何其他参数。虽然我不认为它与List<T>.Sort()一起使用,但仍然使用比较器的方法可以依赖于传入null是安全的。因此:

public int Compare(Product x, Product y)
{
   if(ReferenceEquals(x, y))//either both null or both the same instance
     return 0;
   if(x == null)
     return -1;
   if(y == null)
     return 1;

这可能是相关的。

最后,如果Email字段为空,则可以抛出

return x.Email.CompareTo(y.Email)

最好的办法是在构造函数和setter中使用代码,这样就不可能发生这种情况。删除无参数构造函数,将null检查添加到其他构造函数和检查器,以便在某些内容创建伪造ArgumentNullException时抛出Product而不是稍后。

您还可以向比较器添加检查:

if(x.Email == null || y.Email == null)
  throw new Exception("Cannot compare a user with null email");

这不会修复错误,但可以帮助您追踪错误。