Object reference not set to an instance of an object

1.1k Views Asked by At

I am new to programming, and have come across the following error:

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: 

I have used New to create a new object but still get the same error.

Below is the code:

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
1

There are 1 best solutions below

5
On BEST ANSWER

You need to tell us the line of code that it was thrown on, but my guess is that

Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView)

is dv.DataItem a DataRowView? It returns Nothing if not, and you are not checking that. Otherwise, you do a pretty good job of checking, so there are not many other places it could be.

As an aside: this code

Dim dv As New DetailsView
dv = DirectCast(sender, DetailsView)

Could just be

Dim dv As DetailsView = DirectCast(sender, DetailsView)

No need to create a new one then immediately throw it away. (not 100% sure of my syntax, but don't call New)