Help debugging a NilObjectException

I am getting a NilObjectException in my code. I cannot figure out what I am doing wrong. Is there someplace I can post the xojo code for someone to look at?

First …

  1. are you sure you have instantiated the object in question at some point?
  2. if so, is it in the proper scope for the location where the error is occuring?
  3. are you sure you have not destroyed the object (by code, or loss of scope)

These are the 3 main reasons I have found for that error…
If you believe #1 is correct… place breakpoints where you issue the “NEW” statement and make sure you really go there…before you get to #2

A super handy thing to do is add an unhanded exception handler which logs the issue, in case you can’t catch it in the debugger:

App.UnhandledException(error as exception)
  System.DebugLog (error.Message + error.Stack.join )

Then check the log (In OSX use Console.app, in Win32 get the free DbgView exe)

…and of course you can always post the code here on this forum. It’s helpful to tell us which line it is you’re getting the exception on. If the exception’s in a property of a parent or module do give us as much info as possible…

I found the problem. But I find it rather funny. In RL I do post-sales support for EDA software. I cannot count the number of times my customers have debugged their own problems in the process of creating a testcase for me.

As I was reducing the issue to a simpler implementation to post to the forum, I found the problem

I had a button set index 1 to 5. I was initializing it using an array numbered 0 to 4

Dim Init2() as String = Array( "H","J","K","L","P" ) dim idx As integer If LayoutInitMenu1.ListIndex <> -1 Then If LayoutInitMenu1.Text = "Init 2" then for idx=0 to UBound(Init2) Button(idx+1).Caption=Init2(idx) // corrected code next

:slight_smile: Glad you found it… and it was item #1 on my list… an object that had never been instantiated :slight_smile:

And make sure “Break on Exceptions” in the Project menu is checked. This will display the line of code causing the exception in the debugger when you run from Xojo.