There are similar questions around here but none that fit into my my particular case scenario.
I have a Windows Form with a Button. The button is attached to the event handler as follows:
private void mybutton_Click(object sender, EventArgs e)
{
// do some processing here
}
In addition there is a combobox where a change in selection in supposed to trigger the button event handler as defined above.
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
mybutton_Click(sender, e); // this is the line which pops up the dialog
}
The code works exactly as intended at runtime but i get a dialog prompt at compile time which reads:
object reference not set to an instance of an object
There are no other errors or warning.
A google search tells me that this message is an error caused if the program is trying to access a member of a reference type variable which is set to null.
However when i run this code in debug mode, both the sender and event(e) variables are not null.
So why is this dialog popping up ?
And if this had been an error or warning - it should have shown as an error or warning but nothing of that sort happens.
Here's the screenshot:
Edits: Answering Questions Raised in Comments
There are no errors as you can see in the screenshot.
The program works great - just this pop up
The popup is caused by the line:mybutton_Click(sender, e);
in the combobox selectedIndexChanged
function.
The mybutton_Click(sender, e)
does not use any of the arguments sender or e in the processing.
I have not installed any VS extensions either.
To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.
So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.
It is not a good design to call the Click-event
of the button in the SelectedIndexChanged-event
of the ComboBox and this might also be the reason for the error.
Better put your logic in a seperate method and call it in the Click- and the SelectedIndexChanged-event like this:
private void UpdateSomething()
{
// Do whatever you want
}
private void mybutton_Click(object sender, EventArgs e)
{
UpdateSomething();
}
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateSomething();
}
I think what is happening is that the events are firing in Design mode (which I seem to recall happening to me a few times when using WinForms back in the day).
To get around it what I did was handled the form load event, then in that I attached a listener to the SelectedIndexChanged. Then in Design mode the event isnt bound and wont fire, but at run time it is bound.
Something like:
public void form_OnLoaded(object sender, EventArgs e)
{
myComboBox.SelectedIndexChanged += mycombobox_SelectedIndexChanged;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With