Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm getting nullpointerexceptions when I call Application.Exit in C#. The combobox selected index is set to null, but this sets off a series of errors when SelectedIndex_changed() is called during the exit. :sigh: I also get errors when the "SelectedIndex = -1" statement is called--again nullPointerExceptions in SelectedIndex_changed(). How should I handle this? Thanks in advance.
Posted
Comments
Sandeep Mewara 15-Sep-10 14:15pm    
Update from OP: I'm not setting it to null. When dispose() inside Application.Exit) is called, C# throws a null pointer exception.

First of all, how in the world are you setting ComboBox.SelectedIndex to null?

It is an int variable which is a non-nullable type.

If you're setting it to null, then that's your problem.

And if you're setting it to -1 and then getting an error in your SelectedIndex_Changed event, then you need to first check to see if SelectedIndex = -1 and exit the method.
 
Share this answer
 
Comments
envprogrammer 15-Sep-10 14:51pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Check the value of SelectedIndex and skip over the code that errors if it's null or -1.
 
Share this answer
 
Comments
envprogrammer 15-Sep-10 14:51pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Alternatively, create a form level boolean to indicate that the form is closing and set it to true in the form's closing event. Then only run the code in your combo box's changed event handler if that variable is false.
 
Share this answer
 
Comments
envprogrammer 15-Sep-10 14:51pm    
This is what I used and it appears to be the answer.
envprogrammer 15-Sep-10 14:51pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Marc A. Brown 15-Sep-10 14:58pm    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900