You have excluded the method InitializeComponent()
that should be called in the form constructor. This method is declared in the other part of the class (note that the class has the partial
modifier), it is probably in a Form1.Designer.cs
file.
Basically, the InitializeComponent()
method instantiates and creates all components in your form, so this NullReferenceException
is bursting - the button1
variable (like all other components) was not instantiated.
Your builder should look like this:
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
Although it has nothing to do with the error, it is important to say that you do not need to declare your variables as decimal
, you can declare them directly as int
. This will not cause major problems, but I do not see the sense to do so. If you want to use an integer, create a int
variable.