0

I am trying to add number of buttons in container of a Panel in window Form, my implementation is as follows:

protected override void Order_Click(object sender, EventArgs e)
{    
  Menu = new MenuForm();
  CatButtons = new Button[5];
  CatButtons[0] = new Button();
  CatButtons[0].Text = "ljjih";
  CatButtons[0].Click += new System.EventHandler(btn_Click);
  Menu.Cat_Panel.Container.Add(CatButtons[0]);
}

I keep getting System.NullReferenceException at the last line

Menu.Cat_Panel.Container.Add(CatButtons[0]);

Note : CatButtons and Menu are already declared globally

iwahdan
  • 429
  • 1
  • 5
  • 14

2 Answers2

1

I Replaced :

Menu.Cat_Panel.Container.Add(CatButtons[0]);

with:

Menu.Cat_Panel.Controls.Add(CatButtons[0]);

And it Worked

iwahdan
  • 429
  • 1
  • 5
  • 14
0

I might be stating the obvious but does Cat_Panel exists as its not in the code snippet you show ? If it doesnt it would explain the Null Reference exception.

Worth checking the existance of that first.

Ben Whyall
  • 268
  • 3
  • 17