-2

I am trying to build a dummy exercise for my own interest. I have one form (form1) with two textboxes (textBox1, textBox2) and a button.

When I click the button, I want to add the numbers passed to the textboxes. Now, I am trying to complicate things by introducing an Interface with the signatures of the appropriate methods to do the addition (ICalculate) and a class (Calculations) which implements the interface.

Then I have another class called Calc which gets initiated from Form1 by passing an ICalculate object and 2 integers (a,b) in its constructor. Furthermore, the Calc class has a method (addition()) for adding the two integers, using the ICalculate object and the two integers instantiated at the constructor of the class and display the result on a messagebox.

The problem is that the compiler throws an error saying that the ICalculate object has not been initialized (Object reference not set to an instance of an object.)

The code is the following:

public interface ICalculate
    {
    int add(int x, int y);
    int sub(int x, int y);
    }

class Calculations : ICalculate
    {

    public int add(int a, int b)
        {
        return (a + b);
        }
    public int sub(int z, int k)
        {
        return (z - k);
        }
    }

class Calc
    {

    private ICalculate _nc;
    private int _x, _y;

    public Calc(ICalculate nc, int a, int b)
        {
        var _nc = nc;
        _x = a;
        _y = b;

        }
    public void addition()
        {
        MessageBox.Show(_nc.add(_x, _y).ToString());
        }
    }
}

The Form1 Code is the following:

        public Form1()
        {
        InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
        {
        var a = Int32.Parse(textBox1.Text);
        var b = Int32.Parse(textBox2.Text);

        var c = new Calc(new Calculations(), a, b);
        c.addition();
        }
    }

Any help appreciated !

  • 1
    Did you add the var to 'var _nc = nc;' just to test what happens? I would try without var as you have declared _nc already...And step through the code with a debugger – Grantly Dec 17 '17 at 20:52
  • "I am trying to build a dummy exercise for my own interest." No, I don't think you are. – Ian Kemp Dec 17 '17 at 20:55
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Dec 17 '17 at 22:10

1 Answers1

3

The problem is in the constructor of the Calc class.
Specifically, this row:

var _nc = nc;

You should remove the var:

_nc = nc;

What happens is that once you have the var keyword you are actually creating a local variable called _nc in the constructor, and assign the value of nc to it, so the class member called _nc is never initialized.

The compiler (tested on VS 2013) should issue a warning for this:

Field '<your namespace here>.Calc._nc' is never assigned to, and will always have its default value null

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121