Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Object reference not set to an instance of an object.

Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved Visual Basic
helpjsontutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave 10169531
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups