473,511 Members | 16,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB 2008: Help instantiating an array of objects

2 New Member
I am trying to Instantiate (did I spell that right?) and dimension an array of objects but I keep getting a NullReferenceException 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:
Expand|Select|Wrap|Line Numbers
  1.         Dim objProvider(50) As ClientHospital
  2.         objProvider(0) = New ClientHospital()
  3.         Dim ClientReader As SqlDataReader
  4.         Dim ClientCount As Integer
  5.         Dim I As Integer = 0
  6.  
  7.         'Load array
  8.         ClientReader = GetClientInfo()
  9.         If (Not ClientReader Is Nothing) AndAlso ClientReader.HasRows Then
  10.             Do While ClientReader.Read
  11.                 'Initialize obj properties
  12.                 objProvider(I).ProviderID = ClientReader("ProviderID")
  13.                 With objProvider(I)
  14.                     .ProviderID = ClientReader("ProviderID")
  15.                     .SysID = ClientReader(1)
  16.                     .Name = ClientReader(2)
  17.                     .Processed = False
  18.                 End With
  19.                 lstProviders.Items.Add(objProvider(I).Name)
  20.                 I = I + 1
  21.             Loop
  22.         End If
  23.  
Does anyone see what I am doing wrong? I have been banging my head against this wall all day. HELP!
Aug 8 '08 #1
3 2859
joedeene
583 Contributor
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.
Aug 8 '08 #2
MikeJonny
1 New Member
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
Aug 22 '08 #3
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

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

Immediately after "DO"
write this line:

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

Regards
Veena
Aug 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
7086
by: david | last post by:
I have a question (not sure if just a newbie one, or a stupid one) whose answer I couldn't find on the C# books and tutorials I could put my hands on. Consider the following useless class (could...
2
2181
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
2
2257
by: FredC | last post by:
S Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1...
2
1228
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the...
6
1612
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a...
3
1782
by: DanielNoack | last post by:
Hi, I have a problem that I am unable to get an array of objects to instantiate properly when it is declared with no upper bound. My code is as follows dim mParams() as clsParameters...
3
4931
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where...
3
1650
by: Randy | last post by:
Hi, I was learning about RTTI when I ran across this example. This line, out of the example, confused me. It is declaring a pointer to a base type and instantiating it with a derived class. I...
3
6324
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.