Question Program Acting Wierd

B.P.

Member
Joined
Mar 19, 2011
Messages
7
Programming Experience
Beginner
I do not know to which forum this belongs so I posted it here in general, let me know if I should post it elsewhere. I have a checkedlistbox (cbJun) which has items that move to a data grid(dvEmllst) when checked. To do this I use the following code in the itemcheck even of the cbJun control:
Private Sub cbSoph_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles cbSoph.ItemCheck
Dim name As String = cbSoph.Items(e.Index)
Dim email As String
Dim grade As String = "10"
If e.NewValue = CheckState.Checked Then
If a_SoMemberstable.Rows.Find(name)("Email").ToString() <> "" Then
email = a_SoMemberstable.Rows.Find(name)("Email").ToString()
Else
email = " "
End If
Dim newrow As String() = {name, email, grade}
dvEmllst.Rows.Add(newrow)
e.NewValue = CheckState.Unchecked
cbSoph.Items.Remove(name)
End If
End Sub

While debugging it passes through all of that code perfectly fine. When the Sub ends, however, the debugger moves out of Email(The form we are in) and goes to the Email.showdialog() code in the MainForm(the form which originally opened Email form). At the Email.showdialog() statement, I get a “NullReferenceException was unhandled: Object reference not set to an instance of an object." Does someone know why it is acting like this?
 
The exception has a stack trace, to show you exactly where the exception was thrown. Find the last method call (note the later calls come first in the call stack) that is in your own code and then place a breakpoint on that line. When execution breaks on that line, check to see which reference is Nothing that you didn't expect to be.
 
Ok I tried placing a breakpoint at If e.NewValue = CheckState.Checked Then . I stepped into each line of code looking for variables that have Nothing as a value. And all seem to have a value by the time they are used. Here is the stack trace.
StackTrace:
at System.Windows.Forms.ListBox.ItemArray.GetState(Int32 index, Int32 stateMask)
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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at Key_Club_Project.MainForm.Button2_Click(Object sender, EventArgs e) in C:\Users\Ben\Documents\Visual Studio 2008\Projects\Key Club Project\MainForm.vb:line 140
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Key_Club_Project.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

When the end of the code is reached, the form closes and the error is shown on the form.showdialog() that originally called the form. so I was wondering if the form itself could be the object without an instance. I am not sure if that's possible, but either way I don't know why the form closes.
By the way, I noticed that "name" in the code above referenced the name of the form, so I changed all the "name" to "sname", but I still get the error.
Do you know what I should try next?
 
I have been working with it some more, and I have determined that the program only throws an error when the item clicked is the final item in the checked list box and the error is thrown when the cbSoph.items.remove(name) code is run. Does anyone know why I cannot remove the final item in the checkedlistbox like this and/or a way I can do that?
 
Does the exception have an inner exception, which would show it's own stack trace that includes where the original exception was thrown?

I believe that the ItemCheck event is raised before the item is actually checked. I'm guessing that your code is actually not working the way you think it is already. I'm guessing that, after the ItemCheck event handler completes, the control tries to check the item and, if it was the last item and you removed it, there is no longer an item at that index. That's conjecture but it's an educated guess.
 
That makes sense, and actually that explains some other behavior I have been having with it. Do you know of a way to remove the item once the item has been checked because I cant find an event that does that?
 
Wouldn't it be a better idea to let the user check all the items they want to remove and then click a Button to remove them all in one go? As it stand, if the user checks the wrong item then it's gone and there's no way to get it back. This would also alleviate your current issue.
 
Back
Top