Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I may be incorrectly using the term "Dictionary Array", so please tell me the correct terminology if that is the case. In my code below, what I am referring to as a dictionary array is Dim PdfIndividual(individualCount).

Essentially I am trying to get a lambda function to return a count after it iterates through my dictionary array and finds the value I am trying to get a count for.

I am limited in the framework where this code will eventually run which is why I am using a lambda function in the first place. So while I am sure the code I have presented may not be ideal, I am really just interested in understanding why the error occurs, versus just getting it to work.

Dim relationshipTypeCount As Func(Of Dictionary(Of String, String)(), String, Integer) =
    Function(PdfIndividualArray, relationshipType)
        Dim dictValue As String = ""
        Dim relTypeCountResult As Integer = 0
        For Each pdfIndDict As Dictionary(Of String, String) In PdfIndividualArray
            pdfIndDict.TryGetValue("RelationshipType", dictValue)
            If dictValue = relationshipType Then
                relTypeCountResult += 1
            End If
        Next
        Return relTypeCountResult
    End Function

Dim individualCount As Integer = 5
Dim relType As String = ""
Dim PdfIndividual(individualCount) As Dictionary(Of String, String)

For i As Integer = 0 To individualCount
    If i = 0 Then
         relType = "primary"
    ElseIf i <= 3 Then
         relType = "secondary"
    ElseIf i > 3 Then
         relType = "guest"
    End If
    PdfIndividual(i) = New Dictionary(Of String, String)
    PdfIndividual(i).Add("RelationshipType", relType)

    Dim relCount As Integer = relCount = relationshipTypeCount(PdfIndividual, relType)
Next

The error occurs on the dictionary element in the For...Each of my relationshipTypeCount lambda function.

The error text is:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

pdfIndDict was Nothing.

Screenshot of the error exception enter image description here

question from:https://stackoverflow.com/questions/65644189/system-nullreferenceexception-when-doing-for-each-on-dictionary-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
317 views
Welcome To Ask or Share your Answers For Others

1 Answer

Depending on your targeted .NET version, you can use the following null-conditional form which is more concise:

For Each pdfIndDict As Dictionary(Of String, String) In PdfIndividualArray
    pdfIndDict?.TryGetValue("RelationshipType", dictValue)
    If dictValue = relationshipType Then relTypeCountResult += 1
Next pdfIndDict 

BTW, you can also use the following null check form:

If pdfIndDict IsNot Nothing Then ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...