Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Newbie-嵌套方法还有另一个System.NullReferenceException-Object错误_C# - Fatal编程技术网

C#Newbie-嵌套方法还有另一个System.NullReferenceException-Object错误

C#Newbie-嵌套方法还有另一个System.NullReferenceException-Object错误,c#,C#,为了便于学习,我设置了一个方法来调用另一个方法来显示通过用户输入定义的值。然而,我最终得到了一个严重的错误: System.NullReferenceException-对象引用未设置为对象的实例 有人能解释一下我是怎么造成这个错误的吗;并且,在不太修改代码的情况下(保留嵌套方法),使其正常工作 Program.cs namespace Test { class Program { static void Main(string[] args) {

为了便于学习,我设置了一个方法来调用另一个方法来显示通过用户输入定义的值。然而,我最终得到了一个严重的错误:

System.NullReferenceException-对象引用未设置为对象的实例

有人能解释一下我是怎么造成这个错误的吗;并且,在不太修改代码的情况下(保留嵌套方法),使其正常工作

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();
        }
    }
}
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();
        }
    }
}
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());
        }
    }
}
Parent.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();
        }
    }
}
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();
        }
    }
}
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());
        }
    }
}
Child.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();
        }
    }
}
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();
        }
    }
}
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());
        }
    }
}
引用类型(类)初始化为
null
。在您的
Parent
类型中,您声明
Child
如下:

public Child Child;
这使得
子字段
为空
。您需要以内联方式初始化它。。或者在构造函数中设置它:

public Child Child = new Child();
……或者

public Parent() {
    Child = new Child();
}
..甚至在声明
父对象时内联:

Parent theParent = new Parent() { Child = new Child() };
与此类似的任何其他类型相同。

引用类型(类)初始化为
null
。在您的
Parent
类型中,您声明
Child
如下:

public Child Child;
这使得
子字段
为空
。您需要以内联方式初始化它。。或者在构造函数中设置它:

public Child Child = new Child();
……或者

public Parent() {
    Child = new Child();
}
..甚至在声明
父对象时内联:

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

其他类似的情况也是如此。

您的
子项
null

首先,创建一个新的
父级

Parent theParent = new Parent();
这有一个
公共
字段
子项
(注意:使用属性,而不是公共字段):

正如你所看到的:这是没有证据的

然后你使用这个孩子:

theParent.Child.Name = input;
但是
child
null
!因此,
NullReferenceException

您必须实例化
子字段

public Child Child = new Child();
或者在另一个地方,这取决于你

关于公共字段的旁注:您通过提供对实例成员的直接访问打破了封装。您应该使用getter和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);
   }
}
输出:

杰克


您的
子项
null

首先,创建一个新的
父级

Parent theParent = new Parent();
这有一个
公共
字段
子项
(注意:使用属性,而不是公共字段):

正如你所看到的:这是没有证据的

然后你使用这个孩子:

theParent.Child.Name = input;
但是
child
null
!因此,
NullReferenceException

您必须实例化
子字段

public Child Child = new Child();
或者在另一个地方,这取决于你

关于公共字段的旁注:您通过提供对实例成员的直接访问打破了封装。您应该使用getter和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);
   }
}
输出:

杰克


谢谢当我将“public Child”替换为“public Child=new Child();”时,解决了一个错误。但是,也有相同的错误消息抱怨Child中的行“Console.WriteLine(theParent.Child.Name.ToString());”。cs@MKANET:尝试删除
.ToString()
<代码>名称
已经是一个
字符串
,所以它不会有什么区别。但是如果
string
null
,它将抛出一个NullReferenceException。我已经尝试过了。字符串为空。它不会显示parent.Child.Name的值(来自前面的用户输入)。您可以使用当前的代码和确切的错误消息编辑您的帖子吗?我的确切代码已经发布(减去上面的使用行)。唯一不同的是我用public Child=new Child()替换了public Child;谢谢当我将“public Child”替换为“public Child=new Child();”时,解决了一个错误。但是,也有相同的错误消息抱怨Child中的行“Console.WriteLine(theParent.Child.Name.ToString());”。cs@MKANET:尝试删除
.ToString()
<代码>名称
已经是一个
字符串
,所以它不会有什么区别。但是如果
string
null
,它将抛出一个NullReferenceException。我已经尝试过了。字符串为空。它不会显示parent.Child.Name的值(来自前面的用户输入)。您可以使用当前的代码和确切的错误消息编辑您的帖子吗?我的确切代码已经发布(减去上面的使用行)。唯一不同的是我用public Child=new Child()替换了public Child;谢谢当我将“public Child”替换为“public Child=new Child();”时,解决了一个错误。但是,也有相同的错误消息抱怨Child中的行“Console.WriteLine(theParent.Child.Name.ToString());”。cs@MKANET将您在
家长
中学到的知识应用于
孩子
。如果
Parent.Child
开始时为空,直到您将其设置为一个值,您认为
Child.Name
发生了什么?谢谢。当我将“public Child”替换为“public Child=new Child();”时,解决了一个错误。但是,也有相同的错误消息抱怨Child中的行“Console.WriteLine(theParent.Child.Name.ToString());”。cs@MKANET将您在
家长
中学到的知识应用于
孩子
。如果
Parent.Child
开始时为空,直到您将其设置为一个值,您认为
Child.Name
发生了什么?