我正在使用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;
}
}