System.NullReferenceException when deleting a checkbox from a CheckedListbox
Exception
CheckedListBox1 thru 4
If I click on the last checkbox in checkedListBox3 with text IET350, I get the System.NullReferenceException. If I click on the second to last checkbox in checkedListBox3, I don’t get the exception. However, if I attempt to remove two checkboxes then I get the exception if I click on either of the last two checkboxes in checkedListBox3. If I attempt to remove three checkboxes, the exception is thrown if I click on any of the last three checkboxes in checkedListBox3 and so on.
private void checkedListBox3_ItemCheck(object sender, ItemCheckEventArgs e)
{
checkedListBox3.Items.RemoveAt(1);
}
I have tried debugging, but cannot determine the problem. The checkedListBox3.Items.RemoveAt(1); executes fine. I don’t see a problem in the stack trace.
************* Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.CheckedListBox.CheckedItemCollection.SetCheckedState(Int32 index, CheckState value)
at System.Windows.Forms.CheckedListBox.LbnSelChange()
at System.Windows.Forms.CheckedListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.CheckedListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Jim Collins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.