Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# Add()返回NullReferenceException,即使添加的变量不是null_C#_Dictionary - Fatal编程技术网

C# Add()返回NullReferenceException,即使添加的变量不是null

C# Add()返回NullReferenceException,即使添加的变量不是null,c#,dictionary,C#,Dictionary,我有一个名为Statistics的对象,其中包含3个参数restartsPerBuildD、restartsByReleaseD和buildPerReleaseD,它们属于字典类型 我有一个方法,应该是获取这些变量的信息并填写这些参数,最终将对象添加到列表中。但是,当我尝试将它们添加到字典参数时,系统会提示我出现NullReferenceException。代码如下所示: 编辑: 还在此处添加了统计信息类: public class Statistics { public Dictiona

我有一个名为
Statistics
的对象,其中包含3个参数
restartsPerBuildD
restartsByReleaseD
buildPerReleaseD
,它们属于
字典类型

我有一个方法,应该是获取这些变量的信息并填写这些参数,最终将对象添加到列表中。但是,当我尝试将它们添加到字典参数时,系统会提示我出现
NullReferenceException
。代码如下所示:

编辑:

还在此处添加了
统计信息
类:

public class Statistics
{
   public Dictionary<string, double> restartsPerBuildD { get; set; }
   public Dictionary<string, double> restartsByReleaseD { get; set; }
   public Dictionary<string, double> buildsPerReleaseD { get; set; }
}
公共类统计信息
{
公共字典重新启动sperbuild{get;set;}
公共字典重新启动已删除{get;set;}
公共词典构建重新发布{get;set;}
}
以及功能:

- 在线:

statistics.restartsPerBuildD.Add(rbr.Key,_restartsPerBuild)

我得到:

NullReferenceException


错误与您正在添加的值无关,而是由于您试图添加到的词典尚未初始化

因此,在统计类中,虽然您已经定义了属性,因为它们是字典,但它们也必须初始化

因此,您应该创建一个默认构造函数,或者进行内联操作(见下文)

公共类统计信息
{
公共字典restartsPerBuildD{get;set;}=newdictionary();
公共字典restartsByReleaseD{get;set;}=new Dictionary();
公共字典buildsPerReleaseD{get;set;}=new Dictionary();
}

Jason.kaisersmith是对的,您还没有初始化字典。您可以这样做,也可以在构造函数中初始化字典

选项1

public class Statistics
{
    Statistics()
    {
        restartsPerBuildD = new Dictionary<string, double>();
        restartsByReleaseD = new Dictionary<string, double>();
        buildsPerReleaseD = new Dictionary<string, double>();
    }
   public Dictionary<string, double> restartsPerBuildD { get; set; }
   public Dictionary<string, double> restartsByReleaseD { get; set; }
   public Dictionary<string, double> buildsPerReleaseD { get; set; }
}
公共类统计信息
{
统计数字()
{
restartsPerBuildD=新字典();
restartsByReleaseD=新字典();
buildsperreleed=新字典();
}
公共字典重新启动sperbuild{get;set;}
公共字典重新启动已删除{get;set;}
公共词典构建重新发布{get;set;}
}
选项2

public class Statistics
{
   public Dictionary<string, double> restartsPerBuildD { get; set; }  = new Dictionary<string, double>();
   public Dictionary<string, double> restartsByReleaseD { get; set; } = new Dictionary<string, double>();
   public Dictionary<string, double> buildsPerReleaseD { get; set; } = new Dictionary<string, double>();
}
公共类统计信息
{
公共字典restartsPerBuildD{get;set;}=newdictionary();
公共字典restartsByReleaseD{get;set;}=new Dictionary();
公共字典buildsPerReleaseD{get;set;}=new Dictionary();
}

请为您的统计类添加代码。@jason.kaiserssmith愚弄我,我忘了那个。我用它编辑了OP。您应该编写
公共字典restartsPerBuildD{get;set;}=newdictionary()统计
类中的code>(以及其他行中的模拟更改)。@someone真棒!这很有效。谢谢
public class Statistics
{
   public Dictionary<string, double> restartsPerBuildD { get; set; }  = new Dictionary<string, double>();
   public Dictionary<string, double> restartsByReleaseD { get; set; } = new Dictionary<string, double>();
   public Dictionary<string, double> buildsPerReleaseD { get; set; } = new Dictionary<string, double>();
}