Microsoft KB Archive/890328

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

Article ID: 890328

Article Last Modified on 12/27/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1

-



For users who have the Microsoft .NET Framework 1.1 Service Pack 1 installed
This article discusses the same problem that is discussed in Microsoft Knowledge Base (KB) article 895579. However, the hotfix that this article discusses is only for the original release version of the .NET Framework 1.1. If you have the .NET Framework 1.1 Service Pack 1 (SP1) installed, use the hotfix that is discussed in KB article 895579. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

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



SYMPTOMS
In the Microsoft .NET Framework 1.1, when you merge parent and child menus, a runtime error may occur and you may receive an exception error message that resembles the following:

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

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



Hotfix information
A supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that is described in this article. Apply it only 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 Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer 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.

File information
The English version of this hotfix 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 item in Control Panel.  Date         Time   Version        Size       File name -- 01-Dec-2004  04:08  1.1.4322.1090  1,703,936  System.Design.dll 01-Dec-2004 04:04  1.1.4322.1090    466,944  System.Drawing.dll 01-Dec-2004 04:13  1.1.4322.1090  2,048,000  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 new child Child formChild = new Child; formChild.MdiParent = this;  // set parent formChild.Show;         // display child }

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

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with 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);

}       #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 InitializeComponent call

//

}

///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose;

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with 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;;

}

#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 trying 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 more information about software update terminology, 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: kbqfe kbfix kbbug kbhotfixserver KB890328

-

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

© Microsoft Corporation. All rights reserved.