Results 1 to 7 of 7

Thread: [2005] Form load called on exit...

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    [2005] Form load called on exit...

    I have a form. It is working now. However, when I click the "x" to close the form, I get an error. I put some debug code in. It is occurring in the form Load event. Why would that fire when I am closing the the form?

    The form Load code:
    VB Code:
    1. On Error GoTo errorhandler
    2.         Me.Width = 472
    3.         Me.Height = 283
    4.  
    5.         '===============================
    6.         rsTool = New ADODB.Recordset
    7.         '===============================
    8.         glbSQL = "SELECT [TOOL] FROM TOOLING ORDER BY [TOOL]"
    9.         rsTool.Open(glbSQL, Cnxn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
    10.         If Not rsTool.BOF And Not rsTool.EOF Then
    11.             rsTool.MoveFirst()
    12.             cboTool.Text = rsTool.Fields("tool").Value
    13.             Do
    14.                 cboTool.Items.Add(rsTool.Fields("tool").Value)
    15.                 rsTool.MoveNext()
    16.             Loop While Not rsTool.EOF
    17.         End If
    18.         rsTool.Close()
    19.         rsTool = Nothing
    20.         '=====================
    21.  
    22.         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
    23.         Exit Sub
    24. errorhandler:
    25.         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
    26.         MsgBox("An error occurred: [" & Err.Number & "] " & Err.Description & " - Source: frmToolInq_Load")

    I know I shoudl be using the Try...Catch... code, but I am not that far yet.


    The error message:

    An error occurred [91]: Object reference not set to an insyance of an object. - Source: frmToolInq_Load

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [2005] Form load called on exit...

    Remove the error handler and let the app crash so the debugger will show you exactly what line the exception was thrown on. You can then use the debugger to test each reference on that line to see which is Nothing. You then work backwards to where you expected that object to be created.

    I also strongly suggest that you ditch ADO in favour of ADO.NET.

  3. #3

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Form load called on exit...

    Will do.
    As to the ADO, I want to get a clen runn first, then I want to convert it to "proper" VB2005 programming. I want to use this as a learning template for my other, much larger, systems.

  4. #4

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Form load called on exit...

    It is getting the error on this line:

    VB Code:
    1. cboTool.Items.Add(rsTool.Fields("tool").Value)

    The hint says:

    Use the "new" keyword to create an object instance.

    I don't understand why the form load event is being triggered by a form close event.

  5. #5

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Form load called on exit...

    This is the exception detail:

    System.NullReferenceException was unhandled by user code
    Message="Object reference not set to an instance of an object."
    Source="Tooling"
    StackTrace:
    at Tooling.frmToolInq.frmToolInq_Load(Object eventSender, EventArgs eventArgs) in C:\Documents and Settings\MWalter\My Documents\Visual Studio Projects\tooling\frmToolInq.vb:line 107
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(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.WmShowWindow(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.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
    at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
    at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [2005] Form load called on exit...

    The Load event should ONLY be raised when the form is displayed for the first time. If your event handler is being executed when you are closing the form then the only reasons that I can think of are that you're calling it explicitly, you are creating a new instance of the form somewhere or there is a serious bug in the system. I guess the thing to do is work out which of those is occurring first before worrying about the NullReferenceException. Not much point fixing one bug if it's caused by another and will disappear if that one is fixed.

  7. #7

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Form load called on exit...

    I am going to step through it today and see what is happening. Weird things. I have a form (form1) that calls this form (form2). It uses the "show" command. So, when form2 is opened, form1 is still displayed. Now, when I close form2 (using the "x"), I get the error above and it also closes form1. Weird, eh?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width