Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加到字典时出现NullReferenceException_C#_Dictionary - Fatal编程技术网

C# 添加到字典时出现NullReferenceException

C# 添加到字典时出现NullReferenceException,c#,dictionary,C#,Dictionary,我在用字典。我将仪器作为键,将ordersOfGlass作为值添加到它中。我所有的实例是否都为空?下面的输出证明了什么,但我仍然收到System.NullReferenceException这怎么可能 private Dictionary<Instrument, List<Order>> instrument2Orders = new Dictionary<Instrument, List<Order>>(); .........

我在用字典。我将仪器作为键,将ordersOfGlass作为值添加到它中。我所有的实例是否都为空?下面的输出证明了什么,但我仍然收到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);
            }
        }
仪器类别:

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

第一本能:仪器是空的


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

您的Instrument类是否有可能重写GetHashCode

如果它这样做,并且如果它取消引用空引用,您将得到类似于该错误的结果,尽管堆栈跟踪似乎表明发生了一些不同的情况

无论如何,当您执行dict.Add时,此程序将为您提供NullReferenceException,即使键和值均为非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();
        }
    }
}

上述代码的哪一行引发了异常?您的案例中是否涉及多个线程正在访问字典实例?您是否可以将“бб«б®?谷歌翻译很难想出任何有用的东西。@V4Vendetta你可能是对的。我可能从不同的线程访问字典。@V4Vendetta我修复了线程问题,现在看起来一切都很好。不,null不是字典的有效键;请参阅-但是,Dictionary.Add将在ArgumentNullException为null时抛出ArgumentNullException,而OP的异常为NullReferenceException@MatthewWatson你完全正确,我认为这可能是因为线程问题,可能是其他线程正在修改topicOk的itadded Instrument类,你能提供一个完整的可编译的例子来说明这个问题吗?我让instrument2Orders访问线程安全,这解决了这个问题
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();
        }
    }
}