NilObjectException was not handled

Hi there:

I am new to XOJO and I keep getting “An exception of class NilObjectException was not handled. The application must shut down.” on this block of code:

// get date stuff
d = New Date
s = d.Second
m = d.Minute
h = d.Hour

today = d.shortTime
dateandtime = d.longdate

if temperature <> 11111 then
  TimeOfDayLabel.text = dateandtime + " "+ today+"   |   " + str(Temperature) + "°F"
else
  TimeOfDayLabel.text = dateandtime + " "+ today
end

The program keeps erroring out on this line:
TimeOfDayLabel.text = dateandtime + " "+ today

I am using XOJO 2017 2.1 on Windows 10.

Any guidance would be much appreciated!

Guessing from context, dateandtime and today are both strings, while Temperature is an integer or double. Therefore, the only object that could be nil on that line is TimeOfDayLabel. Examine all 4 values in the debugger. Check to make sure you haven’t dim’d a local variable named TimeOfDayLabel.

In addition to what Tim suggested… Is this code perhaps in the Open event handler of a control on a window, and you are trying to access a property of a different control (TimeOfDayLabel)? If so, move your code to the window’s Open handler, which runs after all the control Open handlers. Only then can you be assured that all controls have been created.

@Urs Geiser good call.

Thanks for the suggestions. This is the entire code on this page. I could not see a dim for TimeOfDayLabel.

Dim s As Integer
Dim m As Integer
Dim h As Integer
Dim d As Date


// get date stuff
d = New Date
s = d.Second
m = d.Minute
h = d.Hour

today = d.shortTime
dateandtime = d.longdate

if temperature <> 11111 then
  TimeOfDayLabel.text = dateandtime + " "+ today+"   |   " + str(Temperature) + "°F"
else
  TimeOfDayLabel.text = dateandtime + " "+ today
end

Canvas2.Refresh(false)

Canvas1.Refresh(false)

PSIMeterCanvas.Refresh(false)

if currentpressure > maxpressure then
  maxpressure = currentPressure
end

if maxPressure >= PSIField.text.val then
  lowerline = true
end

if MaxPressure >= OverPressure then
  lowerline = false
end

If Keyboard.AsyncKeyDown(&h7A) and DebugPressureField.visible = false Then
  DebugPressureField.visible = True
  DebugPressureField.Enabled = true
elseif Keyboard.AsyncKeyDown(&h7A) and DebugPressureField.visible = true then
  DebugPressureField.Visible = False
  DebugPressureField.Enabled = false
end

If Keyboard.AsyncKeyDown(&h6D) Then
  PressureDataListbox.visible = True
end

If Keyboard.AsyncKeyDown(&h6F) Then
  PressureDataListbox.visible = false
end

if SaveSwitch = True Then
  steps = numberofFiles
  call mConvertToPDF
  SaveSwitch = False
end

It look like its missing a variable, but I am not sure what that variable might be as not additional information is provided?
Its in the control section of a Window.

And… what about trapping the Exception (too) ?

What’s this?

if temperature <> 11111 then

Shouldn’t that be &b1111 ?

and there won’t/shouldn’t be one

That should be a CONTROL that was placed on the appropriate window
unless you are dynamically creating it, then you WOULD have a DIM with a NEW parmaeter… but as you indicated this was all the code, we know this isn’t the case.

And if it IS a control on a window… you need to make sure the scope is correct.
If you code is in a Module, then you need to prepend the Label name with the Window Name

Window1.TimeOfDayLabel

But where on the page? What event? If it’s inside a control Open event, then because of control open order, you’re referencing TimOfDayLabel before it has been created/opened. If so, you have 2 options.

  1. Move the code that affects TimeOfDayLabel into the open event of TimeOfDayLabel.
  2. Move all the code to the Window Open event, which fires after all the controls have been created.