1

I'm using instrument2Orders dictionary. I'm adding to it using instrument as a key and ordersOfGlass as value. All my instances or not null what proves output below, but I still receive System.NullReferenceException how is that possible?

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

Output:

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]]

Instrument class:

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;
    }
}
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

2 Answers2

0

First instinct: instrument is null.

Where does this variable come from? It's not a parameter, is it a field?

Davio
  • 4,609
  • 2
  • 31
  • 58
  • 3
    No, null isn't a valid key for a Dictionary; see http://msdn.microsoft.com/en-us/library/k7z0zy8k.aspx - however, Dictionary.Add() will throw ArgumentNullException if it is null, and the OP's exception is NullReferenceException – Matthew Watson May 18 '12 at 09:56
  • 1
    @MatthewWatson You are absolutely correct, i think it may be coz of some threading issue, maybe some other thread is modifying it – V4Vendetta May 18 '12 at 10:00
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();
        }
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276