Noob问题:对象引用未设置为对象的实例。。。C#

Noob问题:对象引用未设置为对象的实例。。。C#,c#,C#,老实说。。。我不知道我做错了什么。。。我得到了错误 对象引用未设置为对象的实例 代码如下所示,我使用//标记了错误 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespa

老实说。。。我不知道我做错了什么。。。我得到了错误

对象引用未设置为对象的实例

代码如下所示,我使用
//
标记了错误

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MonuEventPlanning
{
    public partial class Form1 : Form
    {
        DinnerFun dinnerFun; 

    public Form1()
    {
        InitializeComponent();
        DinnerFun dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
    }

    public void btnCalc_Click(object sender, EventArgs e)
    {
        dinnerFun.CalcDrinks(cbxHealthy.Checked); ///////PROBLEM HERE////////////////
        dinnerFun.CalcDecorations(cbxFancy.Checked);
        DisplayCost(); 
    }

    public void DisplayCost()
    {
        tbxDisplayCost.Text = dinnerFun.CalcTotalCost(cbxHealthy.Checked).ToString("c"); 
    }
}
}

这是我正在进行的实践项目的另一页代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MonuEventPlanning
{
    public partial class Form1 : Form
    {
        DinnerFun dinnerFun; 

    public Form1()
    {
        InitializeComponent();
        DinnerFun dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; //This object is instantiated again?  Or is this the same dinnerFun from above?  
    }

    public void btnCalc_Click(object sender, EventArgs e)
    {
        dinnerFun.CalcDrinks(cbxHealthy.Checked);
        dinnerFun.CalcDecorations(cbxFancy.Checked);
        DisplayCost(); 
    }

    public void DisplayCost()
    {
            tbxDisplayCost.Text = dinnerFun.CalcTotalCost(cbxHealthy.Checked).ToString("c"); 
        }
    }
}
我们将非常感谢您的帮助

问题在于

namespace MonuEventPlanning
{
    public partial class Form1 : Form
    {
        DinnerFun dinnerFun; // class level field declared here

    public Form1()
    {
        InitializeComponent();
        DinnerFun dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
        // ^^^ local declaration, is NOT member field
    }

    public void btnCalc_Click(object sender, EventArgs e)
    {
        dinnerFun.CalcDrinks(cbxHealthy.Checked); // this is the member field, never instantiated
您已经在类级别声明了一个变量,但没有实例化它,而是在构造函数中声明并初始化了一个同名的局部变量。修复它非常简单,只需从构造函数中删除声明,就可以实例化它

这暴露了C#的一个特性。允许局部变量和参数与类的成员字段具有相同的名称。遇到这种情况时,可以通过
this
修饰符引用class字段

class Foo
{
     string bar;
     int baz;

     public Foo(string bar)
     {
          this.bar = bar;
          // ---^ class field
          // ---------^ parameter

          int baz = 42; // local
          this.baz = baz; // assigns local value to class field
     }
}
问题是这个

namespace MonuEventPlanning
{
    public partial class Form1 : Form
    {
        DinnerFun dinnerFun; // class level field declared here

    public Form1()
    {
        InitializeComponent();
        DinnerFun dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
        // ^^^ local declaration, is NOT member field
    }

    public void btnCalc_Click(object sender, EventArgs e)
    {
        dinnerFun.CalcDrinks(cbxHealthy.Checked); // this is the member field, never instantiated
您已经在类级别声明了一个变量,但没有实例化它,而是在构造函数中声明并初始化了一个同名的局部变量。修复它非常简单,只需从构造函数中删除声明,就可以实例化它

这暴露了C#的一个特性。允许局部变量和参数与类的成员字段具有相同的名称。遇到这种情况时,可以通过
this
修饰符引用class字段

class Foo
{
     string bar;
     int baz;

     public Foo(string bar)
     {
          this.bar = bar;
          // ---^ class field
          // ---------^ parameter

          int baz = 42; // local
          this.baz = baz; // assigns local value to class field
     }
}
在Form1()构造函数中,您正在重新声明dinnerFun变量。这将只具有本地作用域,因此永远不会初始化成员级别的dinnerFun

应该是:

public Form1()
{
    InitializeComponent();
    dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}
在Form1()构造函数中,您正在重新声明dinnerFun变量。这将只具有本地作用域,因此永远不会初始化成员级别的dinnerFun

应该是:

public Form1()
{
    InitializeComponent();
    dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}

在构造函数中,您没有将
DinnerFun
对象放入您在表单中声明的
DinnerFun
成员中。您正在构造函数中创建另一个同名的局部变量。当您稍后尝试使用该成员时,它仍然为空

只需从赋值中删除变量声明:

public Form1() {
  InitializeComponent();
  dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}

在构造函数中,您没有将
DinnerFun
对象放入您在表单中声明的
DinnerFun
成员中。您正在构造函数中创建另一个同名的局部变量。当您稍后尝试使用该成员时,它仍然为空

只需从赋值中删除变量声明:

public Form1() {
  InitializeComponent();
  dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}
可能的重复可能的重复