使用List.Add时出现NullReference异常,希望了解一下

本文关键字:了解 希望 一下 异常 NullReference Add List 使用 | 更新日期: 2023-09-27 18:07:04

我有一个设置类,看起来像这样:

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

}

和如下所示的凭据类:

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

我不知道我不知道什么-我应该如何添加新的项目到一个列表,如果他们是预先构建的?

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

看起来您需要实例化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类被实例化时,它将自动创建一个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>();
    }
}