2013-12-09 43 views
0

为了学习的目的,我设置了一个方法来调用另一个方法来显示我通过用户输入定义的值。然而,我最终得到一个讨厌的错误:C#新手 - 嵌套方法又一个System.NullReferenceException - 对象错误

System.NullReferenceException - Object reference not set to an instance of an object

可能有人请解释一下我在做什么引起的错误;并且在不改变代码的情况下进行这项工作(保留嵌套方法)。

的Program.cs

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Parent theParent = new Parent(); 

      Console.WriteLine("Enter Child Name:"); 
      string input = Console.ReadLine(); 
      theParent.Child.Name = input; 

      theParent.FirstMethod(); 
      Console.ReadLine(); 
     } 
    } 
} 

Parent.cs

namespace Test 
{ 
    class Parent 
    { 
     public Child Child = new Child(); //I changed this line. It was originally only 'public Child Child' 

     public void FirstMethod() 
     { 
      Child newChild = new Child(); 
      newChild.SecondMethod(); 
     } 
    } 
} 

Child.cs

namespace Test 
{ 
    class Child 
    { 
     private string name; 

     public string Name 
     { 
      get { return name; } 
      set { name = value; } 
     } 

     public void SecondMethod() 
     { 
      Parent theParent = new Parent(); 
      Console.WriteLine(theParent.Child.Name.ToString()); 
     } 
    } 
} 

回答

1

您的Childnull

首先创建一个新的Parent

Parent theParent = new Parent(); 

这有publicchild(注:使用属性,而不是公共领域):

public Child Child; 

正如你可以看到:这没有证据。

然后你使用这个孩子:

theParent.Child.Name = input; 

childnull!因此,NullReferenceException

你必须instante的child领域:

public Child Child = new Child(); 

或者在另一个地方,那是给你的。

有关公共字段的旁注:您通过提供对实例成员的直接访问来打破封装。您应该使用getters & setter(在C#中方便地由属性提供)。

新情况:

void Main() 
{ 
    Parent theParent = new Parent(); 
    string input = "jack"; 
    theParent.Child.parent = theParent; // ADD THIS 
    theParent.Child.Name = input; 

    theParent.FirstMethod(); 
} 

class Parent 
    { 
     public Child Child = new Child(); //I changed this line. It was originally only 'public Child Child' 

     public void FirstMethod() 
     { 
     // Child.parent = this; // REMOVE THIS 
      Child.SecondMethod(); 
     } 
    } 

class Child 
{ 
    public Parent parent; 
    private string name; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    public void SecondMethod() 
    { 
     Console.WriteLine(parent.Child.Name); 
    } 
} 

输出:

jack

+0

谢谢。当我把“公共儿童儿童”改为“公共儿童儿童=新儿童()”时,那照顾了一个错误。但是,也有同样的错误消息抱怨线“Console.WriteLine(theParent.Child.Name.ToString());”在Child.cs – MKANET

+0

@MKANET:尝试删除'.ToString()'。 'Name'已经是'string'了,所以它不应该有所作为。但是如果'string'是'null',它会抛出一个NullReferenceException。 –

+0

我已经试过了。字符串是空白的。它不会显示给Parent.Child.Name的值(来自之前的用户输入)。 – MKANET

4

引用类型(类)是初始化为null。在你Parent类型,你声明Child这样的:

public Child Child; 

这使得Childnull。您需要内联初始化..或将其设置在构造函数中:

public Child Child = new Child(); 

......或者......

public Parent() { 
    Child = new Child(); 
} 

..或者甚至在线声明Parent对象时:

Parent theParent = new Parent() { Child = new Child() }; 

与其他任何此类相同。

+0

感谢。当我把“公共儿童儿童”改为“公共儿童儿童=新儿童()”时,那照顾了一个错误。但是,也有同样的错误消息抱怨线“Console.WriteLine(theParent.Child.Name.ToString());”在Child.cs – MKANET

+0

@MKANET把你在'Parent'中学到的东西应用到'Child'。如果'Parent.Child'开始为null,直到你将它设置为一个值,你认为'Child.Name'发生了什么? –