VB.net adding to array list error

NCSA SCADA
NCSA SCADA used Ask the Experts™
on
I have a project I am working on that is giving me fits.
I read in a comma seperated file.  loop until EndofStream - works great to this point - I have an object named city with the following properties (name, airfare, carrental, hotel)
i set the value of the properties based on what is read from the text file... everything still works great.... last but not least I add properties to a collection list... well i try... this is where it errors..... +            System.NullReferenceException      {"Object reference not set to an instance of an object."}      System.NullReferenceException

I am just stuck here .. any help would be great
 Private Shared Function Build(ByVal srCity As IO.StreamReader, _
                           ByRef colCities As List(Of City), _
                           ByRef strError As String) As Boolean
        '*************************************************************
        '*  Reads through the City file designated by srFile
        '*  Building the City collection colCities
        '*  Parameters: 
        '*      srCity      stream reader of the City filr
        '*      colCities   collection of cities with names and prices
        '*      strError    If there is an error, it is returned here
        '*  Returns True if file was successfully read
        '*          False if not
        '*************************************************************
        Const c_chrComma As Char = ","c
        Const c_strCityInvalidFormat As String = mc_strCity + " invalid format"


        Dim a_strCity() As String
        Dim objCity As New City


        Try
            Do Until srCity.EndOfStream

                a_strCity = srCity.ReadLine.Split(c_chrComma)


                objCity.Name = a_strCity(0)
                objCity.Airfare = Convert.ToDecimal(a_strCity(1))
                objCity.CarRental = Convert.ToDecimal(a_strCity(2))
                objCity.Hotel = Convert.ToDecimal(a_strCity(3))


                colCities.Add(objCity)
            Loop
            Return True
        Catch ex As Exception
            strError = c_strCityInvalidFormat
            Return False
        Finally
            srCity.Close()
        End Try

    End Function

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
I assume that you get an error here: colCities.Add(objCity)

Add the following:

colCities = new List(Of City)


complete code

Private Shared Function Build(ByVal srCity As IO.StreamReader, _
                           ByRef colCities As List(Of City), _
                           ByRef strError As String) As Boolean
        '*************************************************************
        '*  Reads through the City file designated by srFile
        '*  Building the City collection colCities
        '*  Parameters: 
        '*      srCity      stream reader of the City filr
        '*      colCities   collection of cities with names and prices
        '*      strError    If there is an error, it is returned here
        '*  Returns True if file was successfully read
        '*          False if not
        '*************************************************************
        Const c_chrComma As Char = ","c
        Const c_strCityInvalidFormat As String = mc_strCity + " invalid format"


        Dim a_strCity() As String
        Dim objCity As New City

        colCities = new List(Of City)

        Try
            Do Until srCity.EndOfStream

                a_strCity = srCity.ReadLine.Split(c_chrComma)


                objCity.Name = a_strCity(0)
                objCity.Airfare = Convert.ToDecimal(a_strCity(1))
                objCity.CarRental = Convert.ToDecimal(a_strCity(2))
                objCity.Hotel = Convert.ToDecimal(a_strCity(3))


                colCities.Add(objCity)
            Loop
            Return True
        Catch ex As Exception
            strError = c_strCityInvalidFormat
            Return False
        Finally
            srCity.Close()
        End Try

    End Function

Open in new window

Author

Commented:
Thank you..... I have been trying to figure this out for a few hours... it seems so simple now :)
thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial