2011-02-07 67 views
1

由於某種原因,我得到的對象引用未設置爲對象的實例。未將對象引用設置爲對象的實例

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

+0

我建議的第一件事實際上是調試代碼...啓動它在創建/設置ddate的位置並逐步完成。這似乎是你的'空引用'的錯誤,直到下一行不會擡頭。看看它是否真的從CType()函數中提取一個值。 – guildsbounty 2011-02-07 18:28:43

+1

你應該能夠在第168行放置一個斷點並查看(最有可能的)ddate或ddate.Text爲空。原因將取決於上下文。標籤真的在那裏嗎?你有正確的名字嗎? – 2011-02-07 18:28:45

+2

專業提示:爲您的控件命名。當您試圖記住日期是否在標籤23或標籤17中時,您不會喜歡它。 – 2011-02-07 18:30:18

回答

3

這就是說你沒有在行中被稱爲「label1」的控件。這很可能是正確的,因爲該行只能有Cells。你需要通過其列上一個從零開始的索引來訪問特定的細胞:

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

記住改變上述(2)相匹配的列數。

編輯:看來你還沒有做任何檢查,以確保你正在查看DataRow vs HeaderRow或FooterRow。這裏是一個片段:

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 
3

ddatenull,因爲沒有label1

相關問題