title: HyperLink Error: "Object reference not set to an instance of an object"
body: <p>I've been racking my brain on and off for almost 2 weeks on this one.  I can't figure out why I'm getting this error... any help would be greatly appreciated. </p>
  
  <p>My code behind is as follows: </p>
  
  <pre><code>   Protected Sub TaxSaleGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles TaxSaleGridView.RowDataBound
  
      If e.Row.RowType = DataControlRowType.DataRow Then
  
          Dim LocationMapHyperLink As HyperLink = CType(e.Row.FindControl("OtherParcel"), HyperLink)
  
  
          OPval = DirectCast(e.Row.DataItem, Data.DataRowView)("OtherParcel").ToString()
          PIDval = DirectCast(e.Row.DataItem, Data.DataRowView)("Property_ID").ToString()
  
  
          If Len(OPval) &gt; 1 Then
  
              e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
              LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" &amp; OPval
  
          Else
  
              e.Row.Cells(3).BackColor = System.Drawing.Color.YellowGreen
              LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" &amp; PIDval
  
          End If
  
      End If
  
  End Sub
  </code></pre>
  
  <p>Here is my Markup:</p>
  
  <pre><code> &lt;asp:HyperLink ID="LocationMapHyperLink" runat="server"                
  
      NavigateUrl=""
      Text="Location Map" 
      Target="_blank"&gt;
  
   &lt;/asp:HyperLink&gt;&lt;br /&gt;
  </code></pre>
  
  <p>Here is the error with stack trace:</p>
  
  <pre><code>Object reference not set to an instance of an object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
  Source Error: 
  
  
  Line 359:                e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
  Line 360:
  Line 361:                LocationMapHyperLink.NavigateUrl = "http://www.mydomain.com/index.html?parcel=" &amp; OPval
  Line 362:
  Line 363:                LocationMapHyperLin.NavigateUrl = String.Format("http://www.mydomain.com/index.html?parcel{0}", OPval)
  
  
  Source File: C:\DOTNET\TaxSale\Default.aspx.vb    Line: 361 
  
  Stack Trace: 
  
  
  [NullReferenceException: Object reference not set to an instance of an object.]
     _Default.TaxSaleGridView_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\DOTNET\TaxSale\Default.aspx.vb:361
     System.Web.UI.WebControls.GridViewRowEventHandler.Invoke(Object sender, GridViewRowEventArgs e) +0
     System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[]     fields, TableRowCollection rows, PagedDataSource pagedDataSource) +306
     System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4603
     System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +93
     System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +17
     System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +181
     System.Web.UI.WebControls.DataBoundControl.PerformSelect() +273
     System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +104
     System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +169
     System.Web.UI.Control.EnsureChildControls() +181
     System.Web.UI.WebControls.GridView.get_Rows() +53
     _Default.Page_Load(Object sender, EventArgs e) in C:\DOTNET\TaxSale\Default.aspx.vb:173
     System.Web.UI.Control.LoadRecursive() +70
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3063
  </code></pre>
  
  <p>Here is the markup for TaxSaleGridview:</p>
  
  <pre><code>&lt;asp:GridView ID="TaxSaleGridView" onrowdatabound="TaxSaleGridview_RowDataBound" 
      runat="server" AutoGenerateColumns="False" 
      DataSourceID="TaxSaleDataDS" CellPadding="4" 
      ForeColor="#333333" PageSize="30" HorizontalAlign="Center" 
      AllowPaging="True" ViewStateMode="Enabled"&gt;
      &lt;AlternatingRowStyle BackColor="White" /&gt;
  
      &lt;Columns&gt;        
          &lt;asp:TemplateField HeaderText="Sale ID"&gt;            
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBoxPROPERTYID" runat="server" Text='&lt;%# Bind("PROPERTYID") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
              &lt;ItemStyle HorizontalAlign="center" /&gt;
                  &lt;%Response.Write(startP_bold)%&gt;&lt;asp:Label ID="LabelPROPERTYID" runat="server" Text='&lt;%# Bind("PROPERTYID") %&gt;'&gt;&lt;/asp:Label&gt;&lt;%Response.Write(endP_bold)%&gt;&lt;br /&gt;
  
                  &lt;%If Session(existsOtherParcel) = "N" Then%&gt;
                  &lt;asp:Panel ID="pnlNotOnSale1" runat="server"&gt;
                  &lt;asp:Label ID="lblNotOnSale1" runat="server" Text="***Land is not on sale.***" Font-Bold="True"&gt;&lt;/asp:Label&gt;
                  &lt;/asp:Panel&gt;
  
                  &lt;%Else%&gt;
  
                  &lt;%End If%&gt;
  
  
              &lt;/ItemTemplate&gt;
              &lt;ControlStyle Width="150px" /&gt;
              &lt;HeaderStyle Width="150px" /&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="*Minimum Sale Price" ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center"&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBoxATSALE" runat="server" Text='&lt;%# Bind("ATSALE") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;                                   
                  $&lt;asp:Label ID="LabelATSALE" runat="server" Text='&lt;%# Bind("ATSALE") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>
              </p>
  
  <pre><code>        &lt;asp:TemplateField HeaderText="Property Information and Address" ItemStyle-Width="400"&gt;
  
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBoxNAME1" runat="server" Text='&lt;%# Bind("NAME1") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LabelNAME1" runat="server" Text='&lt;%# Bind("NAME1") %&gt;'&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                  &lt;asp:Label ID="LabelLEGAL_DESC" runat="server" Text='&lt;%# Bind("LEGAL_DESC") %&gt;'&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                  &lt;asp:Label ID="LabelPROP_ADDR1" runat="server" Text='&lt;%# Bind("PROP_ADDR1") %&gt;'&gt;&lt;/asp:Label&gt;, 
                  &lt;%Response.Write(startZ_bold)%&gt;&lt;asp:Label ID="LabelLocation_Zip" runat="server" Text='&lt;%# Bind("Location_Zip") %&gt;'&gt;&lt;/asp:Label&gt;&lt;%Response.Write(endZ_bold)%&gt;&lt;br /&gt;
                  &lt;%Response.Write(startC_bold)%&gt;City: &lt;asp:Label ID="LabelLocation_City" runat="server" Text='&lt;%# Bind("Location_City") %&gt;'&gt;&lt;/asp:Label&gt;&lt;%Response.Write(endC_bold)%&gt;&lt;br /&gt;                    
                  &lt;%Response.Write(startT_bold)%&gt;Taxing District: &lt;asp:Label ID="LabelTax_District_Name" runat="server" Text='&lt;%# Bind("Tax_District_Name") %&gt;'&gt;&lt;/asp:Label&gt;&lt;%Response.Write(endT_bold)%&gt;
              &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>
              </p>
  
  <pre><code>        &lt;%--&lt;asp:BoundField DataField="LEGAL_DESC" HeaderText="LEGAL_DESC" Visible="False" /&gt;
          &lt;asp:BoundField DataField="PROP_ADDR1" HeaderText="PROP_ADDR1" Visible="False" /&gt;            
          &lt;asp:BoundField DataField="Location_City" HeaderText="Location_City" Visible="False" /&gt;
          &lt;asp:BoundField DataField="Location_Zip" HeaderText="Location_Zip" Visible="False" /&gt;
          &lt;asp:BoundField DataField="Tax_District_Name" HeaderText="Tax_District_Name" Visible="False" /&gt;--%&gt;
  
          &lt;asp:TemplateField HeaderText="OtherParcel" Visible="true"&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBoxOtherParcel" runat="server" Text='&lt;%# Bind("OtherParcel") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="LabelOtherParcel" runat="server" Text='&lt;%# Bind("OtherParcel") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Reference Materials" ItemStyle-Width="80px" ShowHeader="True" Visible="True"&gt;
              &lt;ItemTemplate&gt;
              &lt;!--&lt;%If Session(existsOtherParcel) = "Y" Then%&gt;
              OP exists? &lt;%Response.Write(Session(existsOtherParcel))%&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;%End If%&gt;
              OPval = &lt;%Response.Write(OPval)%&gt;&lt;br /&gt;
              PIDval = &lt;%Response.Write(PIDval)%&gt;&lt;br /&gt;&lt;br /&gt;--&gt;
                  &lt;asp:HyperLink ID="LocationMapHyperLink" runat="server"                
  
                   NavigateUrl=" "
                   Text="Location Map" Target="_blank" onclick="javascript:window.open(this.href, '','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=780,height=550,left=20,top=20');"&gt;
  
                  &lt;/asp:HyperLink&gt;&lt;br /&gt;                  
            &lt;br /&gt; &lt;%'Response.Write(hrefvalue)%&gt; &lt;br /&gt;&lt;br /&gt;
                  &lt;asp:HyperLink ID="PRCHyperLink" runat="server" 
                      NavigateUrl='&lt;%# Eval("UnformattedParcelNumber", "http://www2.hamiltoncounty.in.gov/apps/reports/rptpropcard.asp?sparcelno={0}&amp;stemp=&amp;dSearch=houseno&amp;dreport=propcard&amp;report=&amp;dReportName=Property Card") %&gt;' 
                      Text="Property Record Card" Target="_blank" onclick="javascript:window.open(this.href, '','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=780,height=550,left=20,top=20');"&gt;
                  &lt;/asp:HyperLink&gt;
                  &lt;br /&gt;
              &lt;/ItemTemplate&gt;                
             &lt;ItemStyle Width="80px"&gt;&lt;/ItemStyle&gt;
          &lt;/asp:TemplateField&gt;        
      &lt;/Columns&gt;
      &lt;FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /&gt;
      &lt;PagerSettings FirstPageText="First" LastPageText="Last" 
          NextPageText="Next" Position="TopAndBottom" 
          PreviousPageText="Previous" PageButtonCount="1" 
          Mode="NextPreviousFirstLast" FirstPageImageUrl="~/images/misc/first.png" 
          LastPageImageUrl="~/images/misc/last.png" 
          NextPageImageUrl="~/images/misc/next.png" 
          PreviousPageImageUrl="~/images/misc/previous.png" /&gt;
      &lt;PagerStyle ForeColor="#333333" HorizontalAlign="Right" Font-Bold="True" Font-Size="Medium" BackColor="#F5F0DC" /&gt;
      &lt;RowStyle BackColor="#F5F0DC" ForeColor="#333333" /&gt;
      &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#FDF5AC" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#4D0000" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#FCF6C0" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#820000" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>When I comment out the LocationMapHyperLink.NavigateUrl portion of the code behind, I get the gridview to display showing no null values in the OtherParcel column, and the backgrounds of the cells changing based on the repsective value.  </p>
  
  <p>Here is a link to a snippet of the gridview: 
  <a href="http://i1132.photobucket.com/albums/m561/joelschmidt/taxsalegridview.jpg" rel="nofollow">http://i1132.photobucket.com/albums/m561/joelschmidt/taxsalegridview.jpg</a></p>