By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,633 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,633 IT Pros & Developers. It's quick & easy.

MDI Application MenuItems.Add Throws exception

P: n/a
Create a simple MDI form and a child form, and try the following simple code
to add a new menu item on the Edit menu popup:

private void editMenu_Popup(object sender, System.EventArgs e)
{
editMoreMenu.MenuItems.Add("New MI " + count);
count++;
}

and get "An unhandled exception of type 'System.NullReferenceException'
occurred in system.windows.forms.dll" and "
Additional information: Object reference not set to an instance of an object."

Is this a known issue in .NET framework? Any thought is highly appreciated.

Full source code for MDI Form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.MenuItem fileNewMI;
private System.Windows.Forms.MenuItem editMenu;
private System.Windows.Forms.MenuItem editMoreMenu;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;

int count = 0;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

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

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.fileNewMI = new System.Windows.Forms.MenuItem();
this.editMenu = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.editMoreMenu = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenu,
this.editMenu,
this.menuItem3});
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileNewMI});
this.fileMenu.MergeType = System.Windows.Forms.MenuMerge.MergeItems;
this.fileMenu.Text = "File";
//
// fileNewMI
//
this.fileNewMI.Index = 0;
this.fileNewMI.Text = "New";
this.fileNewMI.Click += new System.EventHandler(this.fileNewMI_Click);
//
// editMenu
//
this.editMenu.Index = 1;
this.editMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.editMoreMenu});
this.editMenu.Text = "Edit";
this.editMenu.Popup += new System.EventHandler(this.editMenu_Popup);
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Cut";
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "Copy";
//
// editMoreMenu
//
this.editMoreMenu.Index = 2;
this.editMoreMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem6});
this.editMoreMenu.Text = "More...";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Existing MI 1";
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "Existing MI 2";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "Help";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(600, 506);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "MdiForm";

}
#endregion

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

private void editMenu_Popup(object sender, System.EventArgs e)
{
editMoreMenu.MenuItems.Add("New MI " + count);
count++;
}

private void fileNewMI_Click(object sender, System.EventArgs e)
{
FormChild c = new FormChild();
c.MdiParent = this;
c.Show();
}
}
}
Full trivial source code for the child form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class FormChild : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.MenuItem filePreviewMI;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public FormChild()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

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

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.filePreviewMI = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenu});
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.filePreviewMI});
this.fileMenu.MergeType = System.Windows.Forms.MenuMerge.MergeItems;
this.fileMenu.Text = "File";
//
// filePreviewMI
//
this.filePreviewMI.Index = 0;
this.filePreviewMI.Text = "Preview";
//
// FormChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 394);
this.Menu = this.mainMenu1;
this.Name = "FormChild";
this.Text = "Document";

}
#endregion
}
}

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.