Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 无法合并dict<;字符串,对象>;不带空引用的masterDict_C#_Dictionary_Add_Generic Collections_Idictionary - Fatal编程技术网

C# 无法合并dict<;字符串,对象>;不带空引用的masterDict

C# 无法合并dict<;字符串,对象>;不带空引用的masterDict,c#,dictionary,add,generic-collections,idictionary,C#,Dictionary,Add,Generic Collections,Idictionary,这很简单,但我无法将值附加到我的主词典中。我认为这是因为dict有一个object元素,而不仅仅是一个简单的类型,而且“masterDict.Add”方法的语法不正确。我知道我的一些对象值已设置,一些为空字符串,一些为空,但我不认为这是问题的根源-对象确实存在。它在“newMsg.Value”上崩溃 我的错误: System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not

这很简单,但我无法将值附加到我的主词典中。我认为这是因为dict有一个object元素,而不仅仅是一个简单的类型,而且“masterDict.Add”方法的语法不正确。我知道我的一些对象值已设置,一些为空字符串,一些为空,但我不认为这是问题的根源-对象确实存在。它在“newMsg.Value”上崩溃

我的错误:

System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.

    Example Code:

    public class msg
    {
        public string msgId { get; set; }
        public string msgType { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
        public string dateOfBirth { get; set; }
        public string sex { get; set; }
        public string pAddress { get; set; }
        public string pPhone { get; set; }
        public IEnumerable<string> prodCode { get; set; }
        public string dateOfServiceText { get; set; }
     } 
    public static Dictionary<String, msg> masterDict { get; set; }

    public static Dictionary<String, msg> tmpDict = new Dictionary<String, msg>()
    {
        { "1111", new msg { msgId = "1111", msgType = "DFT", firstName="Sachin" }},
        { "1112", new msg { msgId = "1112", msgType = "DFT", firstName="Dina" }},
        { "1113", new msg { msgId = "1113", msgType = "DFT", firstName="Andy" }}
    };

    public static void mergeDict()
    {

        //now insert your new values into the master
        foreach (var newMsg in tmpDict)
            if (newMsg.Value != null)
                masterDict.Add(newMsg.Key, newMsg.Value);
    }

    static void Main(string[] args)
    {
        mergeDict();
    }
System.NullReferenceException未处理
HResult=-2147467261
Message=对象引用未设置为对象的实例。
示例代码:
公共类消息
{
公共字符串msgId{get;set;}
公共字符串msgType{get;set;}
公共字符串lastName{get;set;}
公共字符串名{get;set;}
公共字符串dateOfBirth{get;set;}
公共字符串sex{get;set;}
公共字符串pAddress{get;set;}
公共字符串pPhone{get;set;}
公共IEnumerable产品代码{get;set;}
公共字符串dateOfServiceText{get;set;}
} 
公共静态字典masterDict{get;set;}
公共静态字典tmpDict=新字典()
{
{“1111”,新消息{msgId=“1111”,msgType=“DFT”,firstName=“Sachin”},
{“1112”,新消息{msgId=“1112”,msgType=“DFT”,firstName=“Dina”},
{“1113”,新消息{msgId=“1113”,msgType=“DFT”,firstName=“Andy”}
};
公共静态void mergeDict()
{
//现在,将新值插入主控形状
foreach(tmpDict中的var newMsg)
如果(newMsg.Value!=null)
masterDict.Add(newMsg.Key,newMsg.Value);
}
静态void Main(字符串[]参数)
{
mergeDict();
}

您从未初始化过您的
masterDict

publicstaticdictionary masterDict{get;set;}

这并没有把它设置成任何东西。在您设置它之前,它将等于null:

masterDict=newdictionary()


您可能希望在类构造函数中执行此操作。

您是正确的-这很有效。通常我会这样做。在本例中,我最初是从“masterDict=msgs.ToDictionary(m=>m.msgId);”之类的内容开始的,出于某种原因,它在没有实例化的情况下工作。当时我没有必要这么做;所以,我忽略了它。