title: How to properly initialize a Windows Forms application
body: <p>I have written this sample program - simplifying a very complex application. The same binary [.exe] throws a null pointer exception on some machines on startup. So, I want to know how to properly construct a Windows Forms form.</p>
  
  <p>In our application the <code>Form1_SizeChanged</code> method is a result of <code>this.ResumeLayout(false)</code> method which is the last statement in <code>InitializeComponents()</code>. I don't know to simulate that, so I just changed the size myself for this test program.</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public class Logger {
          public Logger() { }
          public void log(string str) {
              Console.WriteLine("logging - " + str);
          }
      }
  
      Logger logger = null;
  
      public Form1()
      {
          InitializeComponent();
          this.Size = new Size(200, 300);
          MyInitialize();
      }
  
      private void MyInitialize() {
  
          // Just that it takes some time.
          Console.WriteLine("MyInitialize -- Enter");
          for (int count = 0; count &lt;5 ; count++){
              Console.WriteLine("Sleeping -- " + count);
              Thread.Sleep(1000);
          }
          logger = new Logger();
      }
  
      private void sleepingPill(int millisec) {
          Thread.Sleep(millisec);
      }
  
      private void Form1_SizeChanged(object sender, EventArgs e)
      {
          logger.log("Form1_SizeChanged -- Enter");
      }
  }
  </code></pre>
  
  <p>According to my understanding, <code>Form1_SizeChanged</code> should not be called unless <code>Form1</code> is constructed properly. Can someone throw some light on how do the Windows Forms event architecture work in this scenario?</p>
  
  <pre><code>
  Original Stack Trace: from our complex application
  System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    Source=ABCD
    StackTrace:
         at ABCD.Form1.AppendToLog(String s)
         at ABCD.Form1.Form1_SizeChanged(Object sender, EventArgs e)
         at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
         at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
         at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
         at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
         at System.Windows.Forms.Form.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
         at System.Windows.Forms.Control.ScaleControl(SizeF factor, BoundsSpecified specified)
         at System.Windows.Forms.ScrollableControl.ScaleControl(SizeF factor, BoundsSpecified specified)
         at System.Windows.Forms.Form.ScaleControl(SizeF factor, BoundsSpecified specified)
         at System.Windows.Forms.Control.ScaleControl(SizeF includedFactor, SizeF excludedFactor, Control requestingControl)
         at System.Windows.Forms.ContainerControl.Scale(SizeF includedFactor, SizeF excludedFactor, Control requestingControl)
         at System.Windows.Forms.ContainerControl.PerformAutoScale(Boolean includedBounds, Boolean excludedBounds)
         at System.Windows.Forms.ContainerControl.PerformNeededAutoScaleOnLayout()
         at System.Windows.Forms.ContainerControl.OnLayoutResuming(Boolean performLayout)
         at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
         at ABCD.Form1.InitializeComponent()
         at ABCD.Form1..ctor()
         at ABCD.Program.Main()
    InnerException:
  
  </code></pre>
  
  <p>Notice from the stack trace: Form1_sizeChanged is called from InitializeComponents() .. which I think should not happen. Form1_sizeChanged is an instance method of Form1 class and should not be called before the Form1 is constructed. If the .NET environment want to process this event, it should wait till Form1 is constructed properly, shouldn't it?</p>