FIX: System.NullReferenceException error occurs if you assign a TreeView node tag property (316180)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q316180

SYMPTOMS

If you assign a value to a property of an ActiveX control in Visual Basic .NET, you may receive the following error message when you run your application:
An unhandled exception of type 'System.NullReferenceException' occurred in <project name>.exe

Additional information: Object reference not set to an instance of an object.

Note The example that is used to reproduce this error message uses the ActiveX version of the TreeView control. This error message may occur with other controls.

CAUSE

The problem can occur because the Set method of the control expects a Reference type instead of a Value type, which results in the NullReferenceException. Visual Basic .NET always calls the Set method of a property if it is available. If you want to call the Let method, you need to specify the Let method explicitly.

RESOLUTION

Use one of the following methods to resolve this problem:
  • Use the Visual Basic .NET version of the control. -or-

  • Explicitly tell the compiler to use the Let method. The following code demonstrates the syntax to use:
    AxTreeView1.Nodes.Add(, , , "Node 1")
    AxTreeView1.Nodes(1).let_Tag("MyTag")
    					

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

This bug was corrected in Visual Studio .NET 2003.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows Application in Visual Basic .NET.
  2. Customize the toolbox to add the Treeview control (MSComctl.ocx). The legacy TreeView control appears as TreeCtrl in the toolbox.
  3. Add an instance of TreeCtrl to Form1.
  4. Add the following code to the Load event of Form1:
    AxTreeView1.Nodes.Add(, , , "Node 1")
    AxTreeView1.Nodes(1).Tag = "MyTag"
    					
  5. Build and run the project. You should receive the error message that is listed in the "Symptoms" section.

Modification Type:MinorLast Reviewed:1/20/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbfix kbbug kberrmsg kbpending KB316180