1

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

2 回答 2

0

第一直觉:仪器为空。

这个变量从何而来?不是参数,是字段吗?

于 2012-05-18T09:53:16.150 回答
0

Does your Instrument class override GetHashCode() by any chance?

If it does and if it dereferences a null ref, you'll get something like that error (although the stack trace seems to indicate something different is going on...)

Anyway, this program will give you a NullReferenceException when you do dict.Add() even though the key and value are both non-null:

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();
        }
    }
}
于 2012-05-18T10:17:29.787 回答