1

Honestly... I haven't got a clue what I'm doing wrong... I get the error

Object reference not set to an instance of an object

The code is shown below and I have the marked the error using the /////.

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

}

Here is my other page of code with this practice project I am doing:

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

Some help would be appreciated.

Larry Morries
  • 671
  • 7
  • 17
MonuMan5
  • 373
  • 4
  • 15

3 Answers3

11

The problem is this

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

You have declared a variable at the class level, but you do not instantiate it, you declare and initialize a local variable with the same name in the constructor. It is simple enough to fix, simply remove the declaration from the constructor, just instantiate it.

    public Form1()
    {
        InitializeComponent();
        dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
        // ^^^ instantiates class field
    }

This exposes a feature of C#. Local variables and parameters are allowed to have the same name as member fields of classes. When you run into that situation, you can refer to the class field via the this modifier.

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
     }
}
Anthony Pegram
  • 114,815
  • 25
  • 210
  • 245
2

In the constructor you are not putting the DinnerFun object in the dinnerFun member that you have declared in the form. You are creating another local variable with the same name inside the constructor. When you try to use the member later on, it will still be null.

Just remove the variable declaration from the assignment:

public Form1() {
  InitializeComponent();
  dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}
Guffa
  • 640,220
  • 96
  • 678
  • 956
1

In the Form1() constructor you are redeclaring the dinnerFun variable. This will only have local scope and member level dinnerFun will therefore never be inited.

Should be:

public Form1()
{
    InitializeComponent();
    dinnerFun = new DinnerFun { PeepQty = (int)nudPeepQty.Value }; 
}
Mark
  • 101,574
  • 16
  • 158
  • 219