gpt4 book ai didi

c# - 使用List.Add时出现NullReference异常,想了解一下

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:13 26 4
gpt4 key购买 nike

<分区>

我有一个如下所示的设置类:

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com