System.NullReferenceException in For Each Loop

dbustad

Newcomer
Joined
Nov 25, 2003
Messages
2
I'm working on a small class that grabs TV listings (extensive parsing) from the web and enters it into a usable format. However, when I try to access the information stored in an array in this class, I receive a System.NullReferenceException error (i.e. Object reference not set to an instance of an object). I will include the code here (with some shorthand - the parsing code makes everything look muddled).

The easiest way I found was to find the URLs for the individual networks, and downloading the program information from each network separately. So this is after all the URLs are found for the networks.

Code:
    Public Structure TVProgram
        Public Time As String
        Public Name As String
    End Structure

    Public Structure TVNetwork
        Public ProgramArray() As TVProgram
        Public Name As String
        Friend URL As String
    End Structure

    Public Class TVInfo
        Public Shared OutputText As String
        Public NetworkArray() As TVNetwork
        Private intCount As Integer

        Public Function GetListings() As Boolean

            ' ...
            ' This is where all the URLs are found for the networks
            ' and entered into the NetworkArray array.  I'm not
            ' including it because I'm quite sure it works, and is
            ' ugly.
            ' ...

            For Each i As TVNetwork In NetworkArray
                strHTML = 'html of specific network programming info

                Dim intLoc As Integer = 1
                'Find the network name, i.e. "CBS - Channel 5"
                Do
                    If Mid(strHTML, intLoc, 20) = "<font face=arial><b>" Then
                        For intTemp As Integer = intLoc To Len(strHTML)
                            If Mid(strHTML, inttemp, 4) = "</b>" Then
                                i.Name = Mid(strHTML, intLoc + 20, inttemp - intLoc - 20)
                                Exit Do
                            End If
                        Next intTemp
                    End If
                    intLoc = intLoc + 1
                Loop While intLoc < Len(strHTML)

                'Find tv program names and times
                Dim intProgramArray As Integer = 0
                ReDim Preserve i.ProgramArray(0)
                Do
                    If Mid(strHTML, intLoc, 10) = "nowrap><b>" Then
                        'times
                        For intTemp As Integer = intLoc To Len(strHTML)
                            If Mid(strHTML, intTemp, 4) = "</b>" Then
                                i.ProgramArray(intProgramArray).Time = Mid(strHTML, intLoc + 10, intTemp - intLoc - 10)
                                Exit For
                            End If
                        Next
                        'names
                        For intTemp As Integer = intLoc To Len(strHTML)
                            If Mid(strHTML, intTemp, 14) = "&.intl=us""><b>" Then
                                For intTemp2 As Integer = intTemp To Len(strHTML)
                                    If Mid(strHTML, intTemp2, 4) = "</b>" Then
                                        i.ProgramArray(intProgramArray).Name = Mid(strHTML, intTemp + 14, intTemp2 - intTemp - 14)
                                        Exit For
                                    End If
                                Next
                                Exit For
                            End If
                        Next
                        ReDim Preserve i.ProgramArray(i.ProgramArray.GetLength(0))
'previous line works because redim is zero-based, while getlength is 1-based
                    End If
                    intLoc = intLoc + 1
                Loop While intLoc < Len(strHTML)
            Next
        End Function
    End Class

So, this seems to work alright, but when I run this code later on:

Code:
        Dim temp As New TVListingsInfo.TVInfo
        temp.GetListings()
        For Each p As TVListingsInfo.TVNetwork In temp.NetworkArray
            TVListingsInfo.TVInfo.AddToText(p.Name)
            TVListingsInfo.TVInfo.AddToText(p.URL)
            For Each q As TVListingsInfo.TVProgram In p.ProgramArray
                TVListingsInfo.TVInfo.AddToText(q.Name)
                TVListingsInfo.TVInfo.AddToText(q.Time)
            Next ' *****
        Next
        TextBox1.Text = TVListingsInfo.TVInfo.OutputText

The error occurs at the *****. This is quite confusing for me because if the object didn't exist, I would think the For Each would not continue... I must be making a mistake somewhere, but I cannot find where. Thank you in advance for any help you can render.

Sincerely,
Dale Bustad
 
Sorry - Ignore the last sentence of the second paragraph. I moved that comment into the appropriate place in the code: it made more sense. Thanks, again.
 
Back
Top