NullReferenceException - Getting the string contained in each item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcmahon
    New Member
    • Feb 2009
    • 9

    NullReferenceException - Getting the string contained in each item

    Hi,
    I keep getting the error " Object reference not set to an instance of an object." in relation to the line : selA = CType(lstStuden ts.Items(r.Inde x), String)

    Any advice/help?

    Code:
                Dim BRow As DataRow
                Dim i As Integer
                Dim selA As String
                selA = CType(lstStudents.Items(r.Index), String)
                BRow = objDataSet.Tables("tblStudent").Rows.Find(selA)
                Do While selA <> ""
                    If BRow IsNot Nothing Then
                        Dim studentRows() As DataRow = BRow.Item("StudentID")
                        If studentRows.Length > 0 Then
                            For Each student As DataRow In studentRows
                                Dim strID As Integer = student("StudentID")
                                'add this item
                                objCurrentRow1.Item("StudentID") = strID
                            Next
                        End If
                    End If
                    i = i + 1
                Loop
    'r' has been declared earlier as
    Code:
    Dim r As MeasureItemEventArgs
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Are you sure it returns a string type for every index?
    You have possibly a problem in a few places.
    Looking at: selA = CType(lstStuden ts.Items(r.Inde x), String)
    Is "r" always a value, or is it Nothing(null)?
    Is "r.Index" always a valid number in the range of lstStudents, or does it sometimes = -1?
    Does lstStudents.Ite ms(r.Index) always produce a string type? Have you checked .GetType() to make sure its a string?

    Comment

    Working...