Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting the message "Object reference not set to an instance of an object."

I know I am not initializing or defining the variable chgbtch 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
Posted

You do initialize chgbtch when you do:
VB
Dim chgbtch As New ChargeBatch


The error is likely coming on
VB
chgbtch.ChargeItems(iMyChrgItem).OwnerKey

because ChargeItems has not been intialized.

There are a couple of ways of handling this and you decide which works better for you.

After you initialize chgbtch you could also initialize ChargeItems
VB
chgbtch.ChargeItems = New List...


or you can create a Constructor for your Class and in the constructor initialize all of the needed variables.
 
Share this answer
 
Comments
Dave-10169531 9-Nov-15 13:17pm    
Thank you I knew it was something simple.
ZurdoDev 9-Nov-15 13:35pm    
You're welcome.
You need to add a constructor to initialise any reference properties within your class. Something like:
VB
Public Class ChargeBatch
        Public Property ChargeItems As List(Of ChargeItem)
        Public Property CommunityKey As Long
        Public Property EffectiveDate As String

        Public Sub New()
            ChargeItems = new List(Of ChargeItem)
        End Sub
    End Class

Then in your code, you will need to add a new ChargeItem to the list for each record before trying to set its properties.

NB I don't program VB.NET so you need to verify the syntax above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900