1

I have a settings class that looks like the following:

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


}

And a credentials class that looks like the following:

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

I've built the following to try to add a new credential to my list:

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

And when I do, I'm receiving a null reference exception on the following line:

settings.Credentials.Add(credentials);

I don't know what I don't know - how should I be adding new items into a list if they're prebuilt?

Michael A
  • 9,480
  • 22
  • 70
  • 114
  • 5
    Take a look at your `Credentials` List. It is not initialized ;) Maybe yur JSON has no values for the Credentials and sets them to `null` – ckruczek Aug 10 '15 at 07:39
  • 2
    Public instance fields...Argh, my eyes!!!! – rexcfnghk Aug 10 '15 at 07:44
  • @rexcfnghk Any guidance on how to do it better? Would love to improve my approaches! Should I use backing fields everywhere? If so, why? – Michael A Aug 10 '15 at 07:48
  • @Codingo, You can take a look at the [Field design](https://msdn.microsoft.com/en-us/library/ms229057(v=vs.110).aspx) and [Property design](https://msdn.microsoft.com/en-us/library/ms229006(v=vs.110).aspx) section of the Framework Design Guidelines – rexcfnghk Aug 10 '15 at 07:59
  • If you are using C#6 syntax, you can easily convert your collection fields to properties, something like `public List Jobs { get; set; } = new List();`, although I will go a bit further and remove the setter and type it to `IList` or even `IEnumerable`. – rexcfnghk Aug 10 '15 at 08:02
  • I was simply hoping to make a small joke out of it. No offense intended :) – rexcfnghk Aug 10 '15 at 08:11

2 Answers2

3

It looks like you need to instantiate the Credentials list.

settings.Credentials = new List<PipelineCredentials>

somewhere in the code. Or, to keep things tidy, you could do this in the constructor:

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

That way, whenever a new PipelineSettings class is instantiated, it will automatically create a PipelineCredentials list.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
1

Your Credentials list is not initialized, you could do that in the constructor:

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>();
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939