Search code examples
.netvb.netnullreferenceexceptiontooltip

NullReferenceException with ToolTip.CreateHandle


We sporadically get errors when we try to update a tool tip, like this

ToolTip.SetToolTip(myLabel, customDataStructure)

The error we get is a NullReferenceException: Object reference not set to an instance of an object

Does anyone know the cause of this?

Is a simple Try/Catch that eats the NullReferenceException a feasible option? We don't want our entire application to blow up when we get this.


Solution

  • I would guess that you're calling ToolTip.SetTooltip from an event handler and that handler sometimes fires before the label has been created. You should probably guard this with a check for the label being null and then make sure that the tool tip is initialised on the label's Load event.

    You certainly shouldn't just catch the exception as this hides the problem.