0

While in the same namespace, I have a not-so-complex series of classes on properties, like this:

    public class WholeBase
{
    public SomeHeaders Headers { get; set; }
    public SomeBody Body { get; set; }
}

public partial class SomeHeaders
{
    public string HeaderOne { get; set; } = "N/a";
    public string HeaderTwo { get; set; } = "N/a";
}

public partial class InSet
{
    public string AllForward { get; set; } = "NO";
    public string Available { get; set; } = "NO";
}

public partial class SomeBody
{
    public InSet MySet { get; internal set; }
    public Boolean CombinedServiceIndicator { get; set; } = false;
    public int FrequencyPerDay { get; set; } = 1;
    public string ValidUntil { get; set; } = "9999-12-31";
}

Now, inside Main I try to give values and I get System.NullReferenceException: 'Object reference not set to an instance of an object.' Any ideas?

Assigning values:

    class Program
{
    //...
    static void Main(string[] args)
    {
        WholeBase NewThing = new WholeBase();

        NewThing.SomeHeaders.HeaderOne = "First header Value";

        Console.WriteLine(NewThing.SomeHeaders.HeaderOne.ToString());   //System.NullReferenceException: 'Object reference not set to an instance of an object.'
        Console.ReadLine();

        NewThing.SomeHeaders.HeaderTwo = "Second header Value";
        NewThing.Body.MySet.AllForward = "YES";
        NewThing.Body.MySet.Available = "YES";
        NewThing.Body.CombinedServiceIndicator = false;
        NewThing.Body.FrequencyPerDay = 10;
        NewThing.Body.ValidUntil = "2019-12-31";
    }
}
Nick
  • 483
  • 1
  • 6
  • 15
  • 4
    Your properties (`SomeHeaders`, `Body`, etc) are not instantiated, so they're null. Trying to access the properties of a null object will throw a null reference exception. – Jonathon Chase Dec 17 '19 at 19:27
  • Your code is hard to follow. Particularly `NewThing.SomeHeaders.HeaderOne = "First header Value";` The `NewThing` variable is a `WholeBase`. That class does not have a `SomeHeaders` property. It has a `Headers` property of type `SomeHeaders` (which is never initialized). Does your code compile? – Flydog57 Dec 17 '19 at 19:55
  • That's what I don't understand, of course i am wrong. Thing is, I instantiate the WholeBase (in Main: WholeBase NewThing = new WholeBase(); ) and I see on compile time that I can pass values to all the properties beneath, and no errors are being thrown. *However*, the code does not compile (kind of obvious, from the title of the question..) So, what do I have to correct to make the Main code work?.... – Nick Dec 18 '19 at 09:45
  • `public static void Main(string[] args) { WholeBase NewThing = new WholeBase(); NewThing.Headers = new SomeHeaders { HeaderOne = "aValue", HeaderTwo = "bValue" }; NewThing.Body = new SomeBody { MySet = new InSet { AllForward = "Y", Available = "Y" }, CombinedServiceIndicator = false, FrequencyPerDay = 1, ValidUntil = "2019-12-31" }; } } }` – Nick Dec 18 '19 at 10:54

0 Answers0