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

7
  • Seems that Menu.Cat_Panel (or its Container) is null. Commented Mar 19, 2015 at 22:49
  • Who takes care to initialize the Cat_Panel inside the MenuForm()? Commented Mar 19, 2015 at 22:50
  • Initialized in MenuForms by : Commented Mar 19, 2015 at 22:52
  • this.Cat_Panel = new System.Windows.Forms.Panel(); Commented Mar 19, 2015 at 22:52
  • 2
    But the button should be added to the Controls collection not to the Container. Right? Commented Mar 19, 2015 at 23:04

2 Answers 2

1

I Replaced :

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

with:

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

And it Worked

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.