的NullReferenceException是未处理由用户代码 - 对象引用不设置到对象实例 [英] NullReferenceException was unhandled by user code - Object reference not set to instance of an object

查看:467
本文介绍了的NullReferenceException是未处理由用户代码 - 对象引用不设置到对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C#类:

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

要归纳起来,有区域设置区域,按钮和字段。全区有中心和东。中心和东有产权。 。字段具有具有属性名字,姓氏和chooseLocale标签

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.

在一个方法(叫的getLocale)我有以下代码:

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

当我运行的代码,一个NullReferenceException异常是由用户代码未处理的被扔在该行: englishLang.region.center.title =中心地区;

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

我做得不对的路上我已设置的属性称号,保存,名字,姓氏和chooseLocale?
我试过后区域设置englishLang =新的语言环境()加入以下代码块; 和前 englishLang.region.center.title = 中心地区; ,但我仍然得到错误信息

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?

推荐答案

区域设置对象实例化从来没有它的属性,也没有消费的代码实例化它们。作为引用类型,在类的属性有的默认值。所以,当你做到这一点:

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



下面的值是

englishLang.region
englishLang.buttons
englishLang.fields

因此,您会收到一个的NullReferenceException 如果你试图去参考这些领域里,如你在这里做的:

Thus, you'll receive a NullReferenceException if you try to de-reference those fields, like you do here:

englishLang.region.center.title = "Center Region";



这行代码试图取消引用 englishLang.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.

实例那些在这些DTO类的情况下,最好的地方很可能会在它们的构造。事情是这样的:

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.

这篇关于的NullReferenceException是未处理由用户代码 - 对象引用不设置到对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆