访问构造函数外部的属性时出现NullReferenceException

ano*_*ard 0 c# constructor properties nullreferenceexception

在这个类中,我设置elpElType在构造函数中.

我可以elp在构造函数中访问精细属性(该// ...位是我访问其elp属性的位置),但是当我尝试访问elp另一种方法时ucp()- 我的程序崩溃了NullReferenceException.

我无法弄清楚我在这里做错了什么,虽然它可能是非常直接的.

[Serializable]
public class ElBase : RectangleNode
{
    public ElementParameters elp;

    public ElBase(ElementParameters ElType)
    {
        this.elp = ElType;

        // ...
    }

    private void ucp()
    {
        int i = 0;

        if (this.elp.HasInput)
        {
            // ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sam*_*ell 5

或者:

  1. elp 被设置为null,甚至可能在构造函数中,如果那里没有空检查
  2. HasInput属性的getter被投掷NullReferenceException基于其内部的代码.
  3. 哦,亲爱的:elp是一个可公开访问的领域.任何东西都可以设置为null.:o这应该是数字1,但我直接看过它,因为没有人可以公开访问字段,因此它从来都不是问题.