In Asp.net 'Object reference not set to an instance of an object' error onselectedindexchange of a radiobuttonlist which is inside a gridview
<p>My design page has a <code>RadioButtonList</code> in the item template which has a event <code>OnSelectedIndexChanged</code>. If this event is fired, I get the error</p>

<pre><code>object reference not set to an instance of an object
</code></pre>

<p>That is, I’m getting null value in the radiobutton list even if I have set the values.</p>

<pre><code>&lt;asp:GridView ID="gdvHomeGenlist" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#3366CC" BorderStyle="None"
    BorderWidth="1px" CellPadding="4" Height="12px"
    ShowFooter="True" Width="335px"
    OnRowCommand="gdvHomeGenlist_RowCommand"&gt;
  &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText="Attendance"&gt;
      &lt;ControlStyle Width="200px" /&gt;
      &lt;HeaderStyle Font-Bold="True" Font-Size="8pt" Width="10px" /&gt;
      &lt;ItemTemplate&gt;             
        &lt;asp:RadioButtonList ID="rdbattnd" runat="server"
            RepeatDirection="Horizontal"
            OnSelectedIndexChanged="rdbattnd_SelectedIndexChanged"
            AutoPostBack="True"&gt;
          &lt;asp:ListItem  Value="1"&gt;Attended&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="2"&gt;Not attended&lt;/asp:ListItem&gt;
        &lt;/asp:RadioButtonList&gt;
        &lt;asp:TextBox ID="txtbxHmListGenRsnForNotAttdSpcify" runat="server" &gt;&lt;/asp:TextBox&gt;
      &lt;/ItemTemplate&gt;
      &lt;ItemStyle Font-Size="8pt" /&gt;
    &lt;/asp:TemplateField&gt; 
  &lt;/Columns&gt;
  &lt;RowStyle BackColor="#EFF3FB" /&gt;
  &lt;EditRowStyle BackColor="#2461BF" /&gt;
  &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
  &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
  &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
  &lt;AlternatingRowStyle BackColor="White" /&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p>Code behind:</p>

<pre><code>protected void rdbattnd_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList rdbAttendance =
      (RadioButtonList)gdvHomeGenlist.FindControl("rdbattnd");    ///fetching null   

    /// error object reference is not set to an
    /// instance of an object
    if (rdbAttendance.SelectedValue == "1")
    {
            // some action
    }
}
</code></pre>
