-2

I have this code here:

public ControlCompareDetailDialog(xml.Control control_Sheet1, xml.Control control_Sheet2)
{
    control_Sheet1.TYPE = textBox1.Text;
    control_Sheet2.TYPE = textBox2.Text;

    InitializeComponent();
}

I'm trying to execute it but i get:

Object reference not set to an object instance

Any idea why this happens?

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
MAL
  • 29
  • 8
  • 2
    That's fairly guessable, you need to move that code *after* InitializeComponent so that textBox1 etc actually have a value. Surely the debugger shows you that. – Hans Passant Sep 08 '14 at 10:06

1 Answers1

3

YOu have to put them after InitializeComponent() as it initializes the controls of the form :

InitializeComponent();

 control_Sheet1.TYPE = textBox1.Text;
control_Sheet2.TYPE = textBox2.Text;

See this SO post to understand what InitializeComponent() does

InitializeComponent() method in Visual Studio.NET C# or VB.NET is method that is automatically created and managed by Windows Forms designer and it defines everything you see on the form. Everything done on the form in VS.NET using designers generates code. Every single control added and property set will generate code and that code goes into InitializeComponent() method.

Community
  • 1
  • 1
Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146