title: Error when closing form 2 on C# application - NullReferenceException was unhandled
body: <p>I am trying to refresh my datagrid on <code>Form1</code> each time <code>Form2</code> is closed, but keep getting following error message: <code>NullReferenceException was handled. Object reference not set to an instance of an object.</code> This happens at the line <code>Form1Instance.RefreshGridView();</code> on <code>Form2</code>. Any idea how I can resolve this? My code below:</p>
  
  <p><strong>Form1.cs</strong></p>
  
  <pre><code>    public void RefreshGridView()
      {
          BindingSource bs = new BindingSource();
          bs.DataSource = dataGridView1.DataSource;
          dataGridView1.DataSource = bs;
      }
  </code></pre>
  
  <p><strong>Form2.cs</strong></p>
  
  <pre><code>    private Form1 Form1Instance
      {
          get;
          set;
      }
  
      public Form2(Form1 form1Instance)
      {
          Form1Instance = form1Instance;
      }
  
      private void Form2_FormClosed(object sender, FormClosedEventArgs e)
      {
          Form1Instance.RefreshGridView();
      }
  </code></pre>
  
  <p><strong>Exception Detail</strong></p>
  
  <p><code>System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    Source=PGPTool
    StackTrace:
         at PGPTool.Form2.Form2_FormClosed(Object sender, FormClosedEventArgs e) in C:\Users\Antoine\Documents\Visual Studio 2010\Projects\PGPTool\PGPTool\Form2.cs:line 104
         at System.Windows.Forms.Form.OnFormClosed(FormClosedEventArgs e)
         at System.Windows.Forms.Form.WmClose(Message&amp; m)
         at System.Windows.Forms.Form.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
         at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
         at System.Windows.Forms.Form.Close()
         at PGPTool.Form2.save_btn_Click(Object sender, EventArgs e) in C:\Users\Antoine\Documents\Visual Studio 2010\Projects\PGPTool\PGPTool\Form2.cs:line 78
         at System.Windows.Forms.Control.OnClick(EventArgs e)
         at System.Windows.Forms.Button.OnClick(EventArgs e)
         at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
         at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
         at System.Windows.Forms.Button.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.Run(Form mainForm)
         at PGPTool.Program.Main() in c:\users\Antoine\documents\visual studio 2010\Projects\PGPTool\PGPTool\Program.cs:line 18
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException:</code></p>