Your Locales
object never instantiates its properties, nor does the consuming code instantiate them. As reference types, the properties in that class have a default value of null
. So when you do this:
Locale englishLang = new Locale();
The following values are null
:
englishLang.region
englishLang.buttons
englishLang.fields
Thus, you'll receive a NullReferenceException
if you try to de-reference those fields, like you do here:
englishLang.region.center.title = "Center Region";
That line of code attempts to de-reference englishLang.region
by referring to its center
property. But region
is null
because it hasn't been instantiated yet.
The best place to instantiate those in the case of these DTO classes would probably be in their constructors. Something like this:
public class Locales
{
public Region region { get; set; }
public Buttons buttons { get; set; }
public Fields fields { get; set; }
public Locales()
{
region = new Region();
buttons = new Buttons();
fields = new Fields();
}
}
That way consuming code doesn't have to do this manually each time, the fields are automatically instantiated by the constructor any time you create an instance of Locales
. Naturally, you'll want to repeat this same pattern for your other objects.