title: c# Windows forms 'Object reference not set to an instance of an object'
body: <p>I come to ask because i'm new using windows forms and i need some help. </p>
  
  <p>I have a Windows Forms project with 2 forms. The MainForm and the InnerForm.</p>
  
  <p>I'm trying to access to a TableLayoutPanel in MainForm from InnerForm to add new rows in this tablelayoutpanel with some actions that happen in the InnerForm.</p>
  
  <p>I have the next code:</p>
  
  <p>MainForm:</p>
  
  <pre><code> public partial class MainForm : Form
   {
      public MainForm()
      {
          InitializeComponent();
  
          TableLayoutPanel panel = tableLayoutPanel1;
  
          panel.ColumnCount = 4;
          panel.RowCount = 1;
          panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
          panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
          panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
          panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
          panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
      }
  
      private void AddInnerForm(string url)
      {
          var inner = new InnerForm(url)
          // more code
      }
  
      public void agregarRow(ConsoleMessageEvents args){
          // some action with tableLayoutPanel1
      }
   }
  </code></pre>
  
  <p>InnerForm:</p>
  
  <pre><code> public partial class InnerForm : UserControl
   {
      MainForm theMain;
  
      public InnerForm(MainForm main)
      {
          theMain = main;
      }
  
      public InnerForm(string url)
      {
          InitializeComponent();
          // more code
      }
  
      private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
      {
         theMain.agregaRow(args);
      }
   }
  </code></pre>
  
  <p>but when I'm debugging the program I get this error:</p>
  
  <pre><code>An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe
  
  Additional information: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>in this line in InnerForm:</p>
  
  <pre><code>private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
  {
     **theMain.agregaRow(args)**;
  }
  </code></pre>
  
  <p>What is the problem here?</p>