title: Object reference not set to an instance of an object (Using Gridviews)
body: <p>Edit: This is not a duplicate question as my problem is a tad bit more complicated than a simple NullReferenceException.</p>
  
  <p>This is the bit of code that is giving me problems:</p>
  
  <pre><code> private void BindData()
  {
      SqlConnection con = new SqlConnection(Connection.constr);
      con.Open();
      string[] Querys = new string[4];
      Querys[0] = "PopulateConstraintTable";
      Querys[1] = "PopulateModuleTable";
      Querys[2] = "PopulateFeatureTable";
      Querys[3] = "PopulateInterfaceTable";
      for (int n = 0; n &lt; 4; n++)
      {
          SqlCommand cmd = new SqlCommand(Querys[n], con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add("@License", SqlDbType.Int).Value = 20000 + MyGlobals.CurrentOrgID;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable Table = new DataTable();
          da.Fill(Table);
          ((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
          ((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();
          cmd.ExecuteNonQuery();
      }
      con.Close();
  }
  </code></pre>
  
  <p>This code SHOULD be populating 4 different Gridviews with the information in those stored procedures, but I get an error at lines </p>
  
  <pre><code> ((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
   ((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();
  </code></pre>
  
  <p>that says:</p>
  
  <pre><code> An exception of type 'System.NullReferenceException' occurred in App_Web_30v1z0on.dll but was not handled in user code
  
   Additional information: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Could someone walk me through what is wrong please? Thank you!</p>
  
  <p>EDIT: Markup - </p>
  
  <pre><code>&lt;asp:GridView 
      ID="GridView1" 
      runat="server" 
      CellPadding="6" 
      GridLines="Horizontal"
      Font-Names="Verdana" 
      Font-Size="10pt" 
      DataKeyNames="ConstraintID" 
      AutoGenerateColumns="false" 
      allowpaging="false"
      Width="730px"&gt;
  
      &lt;EditRowStyle BackColor="#999999" /&gt;
      &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Left" Height="25" Font-Bold="True" /&gt;   
      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
  
      &lt;Columns&gt;...&lt;/Columns&gt;
  
  
  
      &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
      &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
      &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="false" ForeColor="#333333" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#E9E7E2" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#506C8C" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#FFFDF8" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE" /&gt;
  
      &lt;/asp:GridView&gt;
  
  &lt;asp:GridView 
      ID="GridView2" 
      cellpadding="6"
      runat="server" 
      GridLines="Horizontal"
      Font-Names="Verdana" 
      Font-Size="10pt" 
      DataKeyNames="ModuleID" 
      AutoGenerateColumns="False"
      onrowcancelingedit="GridView2_RowCancelingEdit" 
      onrowediting="GridView2_RowEditing" 
      onrowupdating="GridView2_RowUpdating" 
      Width="730px"&gt;
  
  
      &lt;HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" /&gt;            
  
      &lt;Columns&gt;...&lt;/Columns&gt;
  
  &lt;/asp:GridView&gt;
  
  
  &lt;asp:GridView 
      ID="GridView3" 
      runat="server" 
      CellPadding="6" 
      GridLines="Horizontal"
      Font-Names="Verdana" 
      Font-Size="10pt" 
      DataKeyNames="FeatureID" 
      AutoGenerateColumns="false" 
      allowpaging="false"
      onrowcancelingedit="GridView3_RowCancelingEdit" 
      onrowediting="GridView3_RowEditing" 
      onrowupdating="GridView3_RowUpdating" 
      OnRowDataBound="GridView3_RowDataBound"
      Width="730px"&gt;
  
      &lt;EditRowStyle Font-Bold="True" /&gt;
  
      &lt;HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" /&gt;   
  
      &lt;Columns&gt;...&lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  
  
  &lt;asp:GridView 
      ID="GridView4" 
      runat="server" 
      CellPadding="6" 
      GridLines="Horizontal"
      Font-Names="Verdana" 
      Font-Size="10pt" 
      DataKeyNames="InterfaceID" 
      AutoGenerateColumns="false" 
      allowpaging="false"
      onrowcancelingedit="GridView4_RowCancelingEdit" 
      onrowediting="GridView4_RowEditing" 
      onrowupdating="GridView4_RowUpdating" 
      OnRowDataBound="GridView4_RowDataBound"
      Width="730px"&gt;
  
      &lt;EditRowStyle Font-Bold="True" /&gt;
  
      &lt;HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" /&gt;   
  
      &lt;Columns&gt;...&lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong><em>Answer:</em></strong>
  Seeing as how this post was bombarded with people claiming this was a duplicate, and so I can not post an answer, I'm going to put the answer here in an edit (All credit and praise be unto @ConnorsFan) </p>
  
  <p>The solution was replacing the two problem lines of code with </p>
  
  <pre><code> ((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataSource = Table;
   ((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataBind();
  </code></pre>