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
question from:https://stackoverflow.com/questions/65644189/system-nullreferenceexception-when-doing-for-each-on-dictionary-array