Microsoft KB Archive/895579

= FIX: You receive a &quot;System.NullReferenceException&quot; exception when you merge parent menus and child menus in an MDI form-based application in the .NET Framework 1.1 Service Pack 1 =

Article ID: 895579

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1

-





SYMPTOMS
In the Microsoft .NET Framework 1.1 Service Pack 1 (SP1), when you merge parent menus and child menus, a run-time error may occur, and you may receive an exception that is similar to the following:

System.NullReferenceException: Object reference not set to an instance of an object.

Note You may merge parent menus and child menus in a Multiple Document Interface (MDI) form-based application.



RESOLUTION
A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next .NET Framework 1.1 service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Prerequisites
The .NET Framework 1.1

Restart requirement
You do not have to restart the computer after you apply this hotfix.

Hotfix replacement information
This hotfix does not replace any other hotfixes.

The English version of this hangout has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.   Date         Time   Version        Size       File name --  09-Mar-2005  20:14  1.1.4322.2303  2,056,192  System.windows.forms.dll



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the problem
 Start Microsoft Visual Studio .NET 2003. On the File menu, click New, and then click Project. Click Visual C# Projects, click Windows Application, and then click OK.  In Solution Explorer, right-click Form1.cs, click View Code, and then replace all the code that is in Form1.cs with the following code. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace MDIMenuTest { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuMain; private System.Windows.Forms.MenuItem MenuFromMain; /// /// Required designer variable. /// private System.ComponentModel.Container components = null;

public Form1 { // // Required for Windows Form Designer support. // InitializeComponent;

// create a new child. Child formChild = new Child; formChild.MdiParent = this; // set parent formChild.Show; // display child }

/// /// Clean up any resources that are used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose; } } base.Dispose( disposing ); }

/// /// Required method for Designer support. Do not modify /// the contents of this method by using the code editor. /// private void InitializeComponent { this.mainMenu1 = new System.Windows.Forms.MainMenu; this.menuMain = new System.Windows.Forms.MenuItem; this.MenuFromMain = new System.Windows.Forms.MenuItem; // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuMain}); // // menuMain // this.menuMain.Index = 0; this.menuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.MenuFromMain}); this.menuMain.MergeType = System.Windows.Forms.MenuMerge.MergeItems; this.menuMain.Text = &quot;Menu&quot; // // MenuFromMain // this.MenuFromMain.Index = 0; this.MenuFromMain.Text = &quot;MenuFromMain&quot; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = &quot;Form1&quot; this.Text = &quot;Form1&quot; this.Load += new System.EventHandler(this.Form1_Load);
 * 1) region Windows Form Designer generated code

}
 * 1) endregion

/// /// The main entry point for the application. /// [STAThread] static void Main { Application.Run(new Form1); }

} }  On the Project menu, click Add Windows Form, click Windows Form under Templates, name the form Child.cs, and then click Open.</li>  In Solution Explorer, right-click Child.cs, click View Code, and then replace all the code that is in Child.cs with the following code. using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Diagnostics;

namespace MDIMenuTest

{

///

/// Summary description for Child.

///

public class Child : System.Windows.Forms.Form

{

private System.Windows.Forms.MainMenu mainMenu1;

private System.Windows.Forms.MenuItem menuMain;

private System.Windows.Forms.MenuItem MenuFromChild;

private System.Windows.Forms.MenuItem menuItem1;

///

/// Required designer variable.

///

private System.ComponentModel.Container components = null;

public Child

{

//

// Required for Windows Form Designer support.

//

InitializeComponent;

//

// TODO: Add any constructor code after the InitializeComponent call.

//

}

///

/// Clean up any resources that are used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose;

}

}

base.Dispose( disposing );

}


 * 1) region Windows Form Designer generated code

///

/// Required method for Designer support. Do not modify

/// the contents of this method by using the code editor.

///

private void InitializeComponent

{

this.mainMenu1 = new System.Windows.Forms.MainMenu;

this.menuMain = new System.Windows.Forms.MenuItem;

this.MenuFromChild = new System.Windows.Forms.MenuItem;

//

// mainMenu1

//

this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.menuMain});

//

// menuMain

//

this.menuMain.Index = 0;

this.menuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.MenuFromChild,

});

this.menuMain.MergeType = System.Windows.Forms.MenuMerge.MergeItems;

this.menuMain.Text = &quot;Menu&quot;

this.menuMain.Popup += new System.EventHandler(this.menuMain_Popup);

//

// MenuFromChild

//

this.MenuFromChild.Index = 0;

this.MenuFromChild.Text = &quot;MenuFromChild&quot;

//

// Child

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Menu = this.mainMenu1;

this.Name = &quot;Child&quot;

this.Text = &quot;Child&quot;

}


 * 1) endregion

private void menuMain_Popup(object sender, System.EventArgs e)

{

// The goal is to dynamically change the child menu items during run time.

// Then, the new child menu items will be merged into the MDI main menu automatically by the .NET Framework.

// However, the program crashed after it tried to add the following menu item to the MDI menu.

MenuItem menuItem = new MenuItem(&quot;New Menu Item From Child&quot;);

this.menuMain.MenuItems.Add(menuItem);

}

}

} </li> On the Debug menu, click Start to compile and then run the project.</li> In the new application, click any menu choice. You receive the following error message:

Object reference not set to an instance of an object

</li></ol>

For the original release version of the .NET Framework 1.1
This article outlines the same problem that is documented in Microsoft Knowledge Base Article 890328. However, the hotfix that is associated with this article is for the .NET Framework 1.1 SP1. Hotfix 890328 is only for the original release version of the .NET Framework 1.1.

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates

Keywords: kbwinserv2003sp2fix kbhotfixserver kbqfe kbprb kbfix kbbug KB895579

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.