NullReferenceException was unhandled by user code - Object reference not set to instance of an object [duplicate]

岁酱吖の 提交于 2019-12-22 05:57:11

问题


I have the following C# classes:

public class Locales
{
    public Region region { get; set; }
    public Buttons buttons { get; set; }
    public Fields fields { get; set; }
}

public class Region
{
    public Center center { get; set; }
    public East east { get; set; }
}

public class Center
{
    public string title { get; set; }
}

public class East
{
    public string title { get; set; }
}

public class Buttons
{
    public string save { get; set; }
}

public class Fields
{
    public Labels labels { get; set; }
}

public class Labels
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string chooseLocale { get; set; }
}

To sum up, Locales has Region, Buttons and Fields. Region has Center and East. Center and East have property title. Fields has Labels which has properties firstName, lastName and chooseLocale.

In a method (called GetLocale) I have the following code:

Locale englishLang = new Locale(); 
englishLang.region.center.title = "Center Region";
englishLang.region.east.title = "East Region - Form";
englishLang.buttons.save = "Save";
englishLang.fields.labels.firstName = "First Name";
englishLang.fields.labels.lastName = "Last Name";
englishLang.fields.labels.chooseLocale = "Choose Your Locale";

When I run the code, a "NullReferenceException was unhandled by user code" is thrown at the line : englishLang.region.center.title = "Center Region";

Am I doing something wrong in the way I have set the properties title, save, firstName, lastName and chooseLocale? I tried adding the following block of code after Locale englishLang = new Locale(); and before englishLang.region.center.title = "Center Region"; but I still get the error message.

Region region = new Region();
Center center = new Center();
East east = new East();
Buttons buttons = new Buttons();
Fields fields = new Fields();
Labels labels = new Labels();

What am I doing wrong?


回答1:


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.




回答2:


You have to instantiate each and every object:

Locale englishLang = new Locale(); 
englishLang.region = new Region();
englishLang.region.center = new Center();
englishLang.region.center.title = "Center Region";

and so on...

Or you can instantiate the dependent objects in the constructor of the parent class.




回答3:


You have to initialize the properties/sub properties before assigning values:

Locale englishLang = new Locale(); 
englishLang.region = new Region();
englishLang.region.center = new Center();
englishLang.region.center.title = "Center Region";



回答4:


You're using automatic properties, and by default they return null for reference types. You need to initialize the properties, probably in the constructor:

public class Locales
{
    public Locales()
    {
        this.region =  new Region();
        this.buttons = new Buttons();
        this.fields = new Fields();
    }

    public Region region { get; set; }
    public Buttons buttons { get; set; }
    public Fields fields { get; set; }
}

You'll also need to add similar code to the other classes.



来源:https://stackoverflow.com/questions/22408075/nullreferenceexception-was-unhandled-by-user-code-object-reference-not-set-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!