Nil Object Exception on Window close

I am getting a Nil Object Exception on Window close which is very difficult to track down. When I step through the debugger, it just stops - and then when I hit Resume, it crashes.

If I place self.hide in the CancelClose event and then Return True, it doesn’t crash.

I am suspicious the problem is in the LostFocus event of an object but I can’t seem to trap the error.

I created a boolean property called WindowClosing and then set that to True in the CancelClose event, and then in objects with a LostFocus event I entered:

If WindowClosing then Return

at the beginning to hopefully stop this Nil Object Exception but I am still getting this error.

These are tough errors to track down. Are there any hints as to how to catch them?

Thanks.

What is the code in the Close event ?

1 Like

If there are any timers running on the window try explicitly turning them off in the close event.

1 Like

Are you using DataView ? Last version can potentially cause NilObject on windows close that Jim is working on…

I am using DataView however it is not being used on the window that is crashing. Does that matter?

I did try turning off all timers but it had no effect.

If InvoiceModified then
  //s = the text you want displayed
  //sOK - what the OK button says
  //sCancel - what the Alternative button says
  //sExplanation = Additional text or information
  Dim i as Integer
  i = App.CONFIRM("Save Changes?","Save","Don't Save","","")
  If i = 1 then  //Save the Invoice
    If Check_for_Errors = False then
      sInvNumber = SaveInvoice
    Else
      Return True
    End If
  Elseif i = 2 then //Discard changes
    
  Else
    Return True
  End if
Else
  WindowClosing = True
  Timer_CheckGlobal.Mode = timer.ModeOff
  Timer_OptionKey.Mode = timer.ModeOff
End If

Should not…but I would suggest try with an older version, 1.15.5.2 is fine.

It is definitely related to having focus in a cell in a listbox. If I set focus to another control, the crash does not occur.

The ListBox is a Xojo ListBox or a DataView based one ?

It is a Xojo listbox.

It seems like the window close event is firing before the Listbox cell lostfocus event. Then when the lostfocus event fires, it no longer exists.

It looks like I solved the problem by changing focus to a different control in the window close event.

This code works:

If InvoiceModified then
  //s = the text you want displayed
  //sOK - what the OK button says
  //sCancel - what the Alternative button says
  //sExplanation = Additional text or information
  Dim i as Integer
  i = App.CONFIRM("Save Changes?","Save","Don't Save","","")
  If i = 1 then  //Save the Invoice
    If Check_for_Errors = False then
      sInvNumber = SaveInvoice
    Else
      Return True
    End If
  Elseif i = 2 then //Discard changes
    
  Else
    Return True
  End if
Else
  WindowClosing = True
  fldDate.SetFocus
End If
1 Like

I’ve got this EXACT SAME problem but not sure how to implement the “fix” noted here. I have a modal window with a few listboxes (actually DataView) and a few buttons inside the main window. When I edit a cell in the listbox, the program crashes (“An exception of NilObjectException was not handled. The application must shut down.”) when I try to close the modal window.

Any suggestions?

Windows 10
Xojo 2016 r1.1

While I have never encountered this, my interpretation of the replies by @David_Schlam is that you just need to shift the focus to outside the listbox / dataview prior to the close event. So assuming you have another control that can accept focus (e.g. an enabled text field), try adding a CancelClose event to the window and in it place a TextField1.SetFocus, where TextField1 is the name of some control that can accept the focus on each OS.

Thanks for taking the time to respond, Douglas. Unfortunately, I interpreted the “fix” the same way as you and had already tried putting a textfield on the modal window to allow TextField1.SetFocus in the CancelClose … No Love!!!