1
votes

For some reason i am getting an object refrence not set to an instance of an object.

  Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)     
      Dim date1 As Date     
      date1 = Date.Now      
      Dim date2 As Date      

      Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)         
      date2 = Date.Parse(ddate.Text)          
      Dim ts As TimeSpan = date2.Subtract(date1)         
      Dim days As Integer = ts.TotalDays            
      If days <= 14 Then             
        e.Row.ForeColor = System.Drawing.Color.Red         
      ElseIf days > 14 And ts.Days < 30 Then 
        e.Row.ForeColor = System.Drawing.Color.Blue         
      ElseIf days >= 30 Then 
        e.Row.ForeColor = System.Drawing.Color.LightGreen         
    End If         
    End Sub

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 166:
Line 167:        Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)
Line 168:        date2 = Date.Parse(ddate.Text)
Line 169:        Dim ts As TimeSpan = date2.Subtract(date1)
Line 170:        Dim days As Integer = ts.TotalDays

Source File: C:\Documents and Settings\ChrisH\Desktop\AJAXEnabledWebSite18\Default.aspx.vb Line: 168

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] _Default.GridView6_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\Documents and Settings\ChrisH\Desktop\AJAXEnabledWebSite18\Default.aspx.vb:168 System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +108 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +167 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1651 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22 System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +17 System.Web.UI.Control.PreRenderRecursiveInternal() +80 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

2
First thing I'd suggest is actually debugging the code...start it where ddate is created/set and step through. That appears to be your 'null reference' error that isn't rearing its head until the next line. See if it is actually pulling a value from the CType() function.guildsbounty
You should be able to put a breakpoint on line 168 and see that (most likely) ddate or ddate.Text is null. The reason will depend on the context. Is the label really there? Do you have the right name?Dave Swersky
Pro-tip: name your controls. You won't like it when you're trying to remember if the date is in label23 or label17.R. Martinho Fernandes
i changed the name to duedate, and found that date2 is 12:00 am even though i am not using time. and also ddate is nothingMyHeadHurts

2 Answers

3
votes

ddate is null, because there is no label1.

3
votes

It is saying that you do not have a control called "label1" in the row. This is very likely correct since the row can only have Cells. You need to access the specific cell by its column on a zero-based index:

Dim ddate As Label = CType(e.Row.Cells(2).FindControl("label1"), Label)

Keep in mind to change the (2) above to match the column number.

Edit: It appears that you haven't made any checks to make sure that you're looking at a DataRow vs a HeaderRow or FooterRow. Here is a snippet:

Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    ' CHECK THE ROW TYPE HERE. ONLY EXECUTE ON DataRow     
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim date1 As Date     
        date1 = Date.Now      
        Dim date2 As Date      

        Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)         
        date2 = Date.Parse(ddate.Text)          
        Dim ts As TimeSpan = date2.Subtract(date1)         
        Dim days As Integer = ts.TotalDays            
        If days <= 14 Then             
            e.Row.ForeColor = System.Drawing.Color.Red         
        ElseIf days > 14 And ts.Days < 30 Then 
            e.Row.ForeColor = System.Drawing.Color.Blue         
        ElseIf days >= 30 Then 
            e.Row.ForeColor = System.Drawing.Color.LightGreen         
        End If         
    End If
End Sub