0
我是新來編程的一個實例,所遇到以下錯誤:對象引用未設置到對象
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_hnmuvsif
StackTrace:
at Default3.DetailsView1_DataBound(Object sender, EventArgs e) in C:\projects\FPOS_v2\photo_view.aspx.vb:line 54
at System.Web.UI.WebControls.BaseDataBoundControl.OnDataBound(EventArgs e)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.DetailsView.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.DetailsView.EnsureDataBound()
at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我已經使用新創建一個新的對象,但仍然得到同樣的錯誤。
下面是代碼:
If IsPostBack Then
Session("selectedPhotoID") = DetailsView1.DataKey.Value.ToString()
DataList1.DataBind()
End If
DetailsView1.Fields(0).Visible = False
DetailsView1.Fields(2).ShowHeader = False
DetailsView1.Fields(3).ShowHeader = False
DetailsView1.Fields(3).ItemStyle.HorizontalAlign = HorizontalAlign.Center
DetailsView1.Fields(3).ItemStyle.Font.Bold = True
DetailsView1.HorizontalAlign = HorizontalAlign.Center
'The code below loops through and selects the photo rather then selecting the first
If Not String.IsNullOrEmpty(Session("selectedPhotoID")) Then
Dim dv As New DetailsView
dv = DirectCast(sender, DetailsView)
If dv IsNot Nothing Then
Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView)
For iRowIndex As Integer = 0 To myDataRowView.DataView.Count - 1
If myDataRowView.DataView(iRowIndex).Row(0).ToString() = Session("selectedPhotoID") Then
'found record
dv.PageIndex = iRowIndex
Exit For
End If
Next
End If
End If
哪一行是拋出的異常? – BoltClock 2011-02-12 22:14:33
參見http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – 2011-02-12 22:29:07