VB 2008: Help instantiating an array of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Glupii
    New Member
    • Aug 2008
    • 2

    #1

    VB 2008: Help instantiating an array of objects

    I am trying to Instantiate (did I spell that right?) and dimension an array of objects but I keep getting a NullReferenceEx ception not handled error telling me to use the "new" keyword to create an object instance. This only happens after the loop has gone through one iteration meaning it is on the second element of the array that it throws this error. Here is my code:
    Code:
            Dim objProvider(50) As ClientHospital
            objProvider(0) = New ClientHospital()
            Dim ClientReader As SqlDataReader
            Dim ClientCount As Integer
            Dim I As Integer = 0
    
            'Load array
            ClientReader = GetClientInfo()
            If (Not ClientReader Is Nothing) AndAlso ClientReader.HasRows Then
                Do While ClientReader.Read
                    'Initialize obj properties
                    objProvider(I).ProviderID = ClientReader("ProviderID")
                    With objProvider(I)
                        .ProviderID = ClientReader("ProviderID")
                        .SysID = ClientReader(1)
                        .Name = ClientReader(2)
                        .Processed = False
                    End With
                    lstProviders.Items.Add(objProvider(I).Name)
                    I = I + 1
                Loop
            End If
    Does anyone see what I am doing wrong? I have been banging my head against this wall all day. HELP!
    Last edited by Glupii; Aug 8 '08, 06:56 PM. Reason: mispelling
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    well im sorry, but i dont have the answer to your question, but you may find it more relevant to post this question under the correct forum such as .NET forum, which visual basic 2008.net is part of the .net framework. perhaps a moderator can move this thread.

    Comment

    • MikeJonny
      New Member
      • Apr 2008
      • 1

      #3
      I think the problems is that you have only instantiated the first object in your array. that is objProvider(0) = New ClientHospital( )

      Try making a loop like
      for i as integer = 0 to 49
      objProvider(i) = New ClientHospital( )
      next

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        Remove this line:
        objProvider(0) = New ClientHospital( )

        Immediately after "DO"
        write this line:

        objProvider(i) = New ClientHospital( )
        objProvider(I). ProviderID = ClientReader("P roviderID")
        ...

        Regards
        Veena

        Comment

        Working...