NullReferenceException with ToolTip.CreateHandle

We get sporadically errors when we try to update the tooltip like

ToolTip.SetToolTip(myLabel, customDataStructure)

      

We get error NullReferenceException: Object reference not set to an instance of an object

Does anyone know the reason for this?

Is it a simple Try/Catch

one that uses a NullReferenceException

valid parameter? We don't want our entire application to explode when we got it.

0


source to share


5 answers


I would assume that you call ToolTip.SetTooltip

from an event handler and that handler sometimes fires before the label is created. You should probably protect this by making sure the label null

is up and then making sure the tool tip is initialized on the label Load

.



You of course shouldn't just catch the exception, as that hides the problem.

+1


source


Try to check if you are setting this variable in any situation using a debugger like ...

Is a simple Try / Catch that eats a NullReferenceException valid?



This will not solve the problem, it will hide it. Bad programming practice.

+1


source


Ignoring exceptions is rarely ever a good idea. The exception is thrown because something is wrong in the current implementation. By ignoring the exception, the application basically goes into undefined state, and you will most likely see other strange effects from the missing link.

Since this is sporadically, it could be a race condition problem, so you need to take a close look at the code to see if there are situations in which this link can be used before it gets properly initialized.

+1


source


The most common reason for this is closing the window and checking, which sets a tooltip for an invisible control.

I haven't debugged the code. Net, but have a pretty good idea where the error is being thrown in the ToolTip.CreateHandle code:

private void CreateHandle()
{
    if (this.GetHandleCreated())
    {
        return;
    }
    IntPtr userCookie = UnsafeNativeMethods.ThemingScope.Activate();
    try
    {
        SafeNativeMethods.InitCommonControlsEx(new NativeMethods.INITCOMMONCONTROLSEX
        {
            dwICC = 8
        });
        CreateParams createParams = this.CreateParams;
        if (this.GetHandleCreated())
        {
            return;
        }
        //HERE! I suspect window is null when the form is closed
        this.window.CreateHandle(createParams); 
    }
    finally
    {
        UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
    }
    if (this.ownerDraw)
    {
        int num = (int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, this.Handle), -16));
        num &= -8388609;
        UnsafeNativeMethods.SetWindowLong(new HandleRef(this, this.Handle), -16, new HandleRef(null, (IntPtr)num));
    }

      

The condition to fix this is very simple, just check if the form is visible or, as Benjamin RAIBAUD mentions Disposing = false before calling ToolTip, controls the SetToolTip method:

FROM#:

if (!this.Disposing) ttpHoverText.SetToolTip(targetControl, brokenText);

      

VB.Net:

If Me.Disposing = False Then ttpHoverText.SetToolTip(targetControl, brokenText)

      

IMO This is what the .NET Framework should handle ...

+1


source


I faced the same problem. The exception seems to be thrown from the ToolTip.CreateHandle () method and it only happens in the MDI child window. Make sure the Disposing property of the parent form is false before calling the SetToolTip (...) method. Anyway, the form is positioned, so you don't need tooltips anymore ...

0


source







All Articles