I am getting the message "Object reference not set to an instance of an object." I know I am not initializing the variable correctly but I cannot find an example that can show me how. I am using the class ChargeBatch with ChargeItem to build data to be passed via a JSON string. I can deserialize in the class fine but when I try to populate the class ChargeItem to serialize it into a JSON string, I get the above error. Thank you in advance for any and all help. Below is my code.
Public Class ChargeBatch
Public Property ChargeItems As List(Of ChargeItem)
Public Property CommunityKey As Long
Public Property EffectiveDate As String
End Class
'Charge Items
Public Class ChargeItem
Public Property OwnerKey() As Long
Public Property ChargeCodeKey() As Long
Public Property Amount() As Double
End Class
Module APIARcharge
Sub Main(ByVal ParamArray args() As String)
Dim iMyChrgItem As Integer = 0
Dim CommID As String = Nothing
Dim ARFile As String = Nothing
If args.Length < 2 Then
Throw New ApplicationException("Invalid or no parameters passed to this module.")
End
Else
CommID = args(0)
ARFile = args(1)
End If
If Not File.Exists(ARFile) Then
Throw New ApplicationException(String.Format("File \[{0}\] does not exist.", ARFile))
End
End If
' Open input file and post
Dim chgbtch As New ChargeBatch
Using mystream As StreamReader = New StreamReader(ARFile)
Dim recs As String
While Not mystream.EndOfStream
recs = mystream.ReadLine().TrimEnd
chgbtch.ChargeItems(iMyChrgItem).OwnerKey = (CInt(Mid(recs, 11, 10)))
chgbtch.ChargeItems(iMyChrgItem).ChargeCodeKey = CInt(Mid(recs, 21, 10))
chgbtch.ChargeItems(iMyChrgItem).Amount = CDbl(Mid(recs, 47, 9)) chgbtch.CommunityKey = CInt(Mid(recs, 1, 10))
chgbtch.EffectiveDate = Format(Mid(recs, 41, 6), "mm/dd/YYYYT00:00:00")
iMyChrgItem = iMyChrgItem + 1
End While
End Using
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(chgbtch)
End Sub