使用List.Add<t>时出现NullReference异常,想了解一下

NullReference exception when using List.Add<t>, would like to understand it

我的设置 class 如下所示:

/// <summary>
/// Class for pipeline settings
/// </summary>
public class PipelineSettings
{
    /// <summary>
    /// List of jobs to process
    /// </summary>
    [JsonProperty("jobs")]
    public List<PipelineJob> Jobs;

    // todo: make private and create FetchCredentials(id)
    /// <summary>
    /// List of credentials information cataloged by id
    /// </summary>
    [JsonProperty("credentials")]
    public List<PipelineCredentials> Credentials;


}

以及如下所示的凭据 class:

/// <summary>
/// Class to hold credentials for pipeline jobs
/// </summary>
public class PipelineCredentials
{
    /// <summary>
    ///  The id for the current credential  to be used when referring to it within
    ///  other areas of json settings files
    /// </summary>
    [JsonProperty("id")]
    public int Id;

    /// <summary>
    /// Username or login string for the current system
    /// </summary>
    [JsonProperty("username")]
    public string Username;

    // refine: AES auto encrypt?
    /// <summary>
    /// The password for the active authentication. If not encrypted then it
    /// will be automatically converted into an AES encrypted string
    /// </summary>
    [JsonProperty("password")]
    public string Password;

    [JsonProperty("path")]
    public string UNCPath;
}

我构建了以下内容以尝试将新凭据添加到我的列表中:

var settings = new PipelineSettings();

// Build credentials for storage
var credentials = new PipelineCredentials();
credentials.Id = 1;
credentials.Username = "testUsername";
credentials.Password = "test_password";
credentials.UNCPath = null;

// Add credentials to the current settings class
settings.Credentials.Add(credentials);

var json = new JsonSerializeHelper();

Console.Write(json.Serialize(settings));

Console.ReadKey();

当我这样做时,我在以下行收到空引用异常:

settings.Credentials.Add(credentials);

我不知道我不知道什么 - 如果新项目是预建的,我应该如何将它们添加到列表中?

看来您需要实例化 Credentials 列表。

settings.Credentials = new List<PipelineCredentials>

代码中的某处。或者,为了保持整洁,您可以在构造函数中执行此操作:

/// <summary>
/// Class for pipeline settings
/// </summary>
public class PipelineSettings
{
    public PipelineSettings()
    {
        this.Credentials = new List<PipelineCredentials>();
    }

    /// <summary>
    /// List of jobs to process
    /// </summary>
    [JsonProperty("jobs")]
    public List<PipelineJob> Jobs;

    // todo: make private and create FetchCredentials(id)
    /// <summary>
    /// List of credentials information cataloged by id
    /// </summary>
    [JsonProperty("credentials")]
    public List<PipelineCredentials> Credentials;
}

这样,每当实例化一个新的 PipelineSettings class 时,它会自动创建一个 PipelineCredentials 列表。

您的 Credentials 列表未初始化,您可以在构造函数中执行此操作:

public class PipelineSettings
{
    /// <summary>
    /// List of jobs to process
    /// </summary>
    [JsonProperty("jobs")]
    public List<PipelineJob> Jobs;

    // todo: make private and create FetchCredentials(id)
    /// <summary>
    /// List of credentials information cataloged by id
    /// </summary>
    [JsonProperty("credentials")]
    public List<PipelineCredentials> Credentials;

    public PipelineSettings()
    {  
        Credentials = new List<PipelineCredentials>();
    }
}