xxyyxx
xxyyxx

Reputation: 2396

Can't figure out why I'm getting the error, "Object reference not set to an instance of an object"

The block of code where I am getting this is,

 If userID > 0 Then
            Dim lq As New lqDFDataContext
            Dim var = lq.mobile_GetCustomChannels(userID).ToList()
            For Each c In var
                Dim channel As New SimpleChannel
                channel.channelID = c.channelID
                channel.userID = c.userID

                If c.matchTitle = True Then
                    channel.matchBy = "Title"
                End If

                If c.matchTitleAbstract = True Then
                    channel.matchBy = "Title and Abstract"
                End If

                If c.fromMyPage = True Then
                    channel.source = "My Page"
                Else
                    channel.source = "All Journals"
                End If

                   Dim tempKW() As String = c.keywords.Split(",")
                For Each kw As String In tempKW
                    channel.keywords.Add(kw)
                Next

                Dim imageKW As String = c.keywords.Substring(0, c.keywords.IndexOf(" "))
                channel.imageURL = SingleImageSearch(imageKW)


                result.Add(channel)
            Next
        End If

The line, "channel.keywords.Add(kw)" is where the error comes up. I've used this technique dozens of time without a problem, and this seems analogous to every other time I've used it yet I keep getting this exception. Can't figure out why.

Upvotes: 1

Views: 107

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564891

It looks like your SimpleChannel constructor isn't initializing the keywords collection. Make sure the constructor is actually creating an instance of keywords (ie: keywords = new List(Of String) or whatever is appropriate).

Upvotes: 1

Related Questions