添加到字典时出现NullReferenceException

时间:2012-05-18 09:50:48

标签: c# dictionary

我正在使用instrument2Orders字典。我使用instrument作为键并ordersOfGlass作为值添加到其中。我的所有实例是否null下面都证明了输出,但我仍然会收到System.NullReferenceException这是怎么可能的?

private Dictionary<Instrument, List<Order>> instrument2Orders = new Dictionary<Instrument, List<Order>>();

.........
        public void InitialRegisterOrder(Order order)
            .....
        if (instrument2Orders.ContainsKey(instrument))
        {
            ordersOfGlass = instrument2Orders[instrument];
        }
        else
        {
            ordersOfGlass = new List<Order>();
            try
            {
                instrument2Orders.Add(instrument, ordersOfGlass);
            } catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.ToString());
                Console.WriteLine(e.InnerException);
                Console.WriteLine("XYZ! instrument = " + instrument + " ordersOfGlass = " + ordersOfGlass + " instrument2Orders = " + instrument2Orders);
            }
        }

输出:

System.NullReferenceException: ‘бл«Є  ­  ®ЎкҐЄв ­Ґ гЄ §лў Ґв ­  нЄ§Ґ¬Ї«па ®ЎкҐЄв .
   ў System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   ў MBClient.Market.InitialRegisterOrder(Order order) ў C:\Oleg\projects\MBClient\MBClient\Market.cs:бва®Є  233

XYZ! instrument = ClassCode: EQNL Ticker: GAZP. ordersOfGlass = System.Collections.Generic.List`1[Commons.Order] instrument2Orders = System.Collections.Generic.Dictionary`2[Commons.Instrument,System.Collections.Generic.List`1[Commons.Order]]

乐器类:

class Instrument
{
    //public Instrument(int id, string classCode, string ticker)
    //{
    //    this.Ticker = ticker;
    //    this.ClassCode = classCode;
    //}

    public string ClassCode { get; set; }
    public string Ticker { get; set; }
    public override string ToString()
    {
        return "ClassCode: " + ClassCode + " Ticker: " + Ticker + '.';
    }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        Instrument instrument = obj as Instrument;
        if (instrument == null)
        {
            return false;
        }
        return (ClassCode.Equals(instrument.ClassCode)) && (Ticker.Equals(instrument.Ticker));
    }

    public override int GetHashCode()
    {
        int hash = 13;
        hash = (hash * 7) + ClassCode.GetHashCode();
        hash = (hash * 7) + Ticker.GetHashCode();
        return hash;
    }
}

2 个答案:

答案 0 :(得分:0)

第一直觉:乐器为空。

这个变量来自哪里?它不是一个参数,是一个字段吗?

答案 1 :(得分:0)

您的Instrument类是否有机会覆盖GetHashCode()?

如果确实如此,如果它取消引用null ref,你会得到类似的错误(尽管堆栈跟踪似乎表明存在不同的情况......)

无论如何,当你执行dict.Add()时,这个程序会给你一个NullReferenceException,即使键和值都是非空的:

using System;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            var dict = new Dictionary<Test, Test>();
            dict.Add(test, test);
        }
    }

    public class Test
    {
        public string Value
        {
            get;
            set;
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }
}