473,668 Members | 2,446 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 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:
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 2865
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("P roviderID")
...

Regards
Veena
Aug 23 '08 #4

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

Similar topics

1
7095
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 be a struct as well, if you just comment out the non static parameterless constructor) and the Main() routine : using System;
2
2189
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 instantiate an array of structures; consider the following useless code : using System; struct MyPointS
2
2268
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 Microsoft Visual C# .NET 69462-335-0000007-18707 Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3 The code below shows the instantiation of multiple V47 objects. These
2
1242
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 documentation wrong (it also says abstract classes cannot be instantiated)? Should I be using Bitmap instead? Is there a difference between Abstract class and Abstract base class?
6
1617
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 class takes a lot of storage or other resources? Would it be a severe performance penalty? From the .Net help doc:
3
1785
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 mParams(0) = new clsParameters produces an error saying mParams is set to nothing however if I change
3
4938
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 vncService is a class name not an refrence or instantiated object. is the above notation is possible or not?if yes how should i declare that class(vncService) so that i can call without instantiating the object. If any of u know pls answer to...
3
1660
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 can say the words ... yet I don't get it. What do I have, a base or a derived? Can anyone push me in the right direction. abc *abc_pointer = new xyz();
3
6345
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 works fine (see below). So what is the problem? I get a null reference on the line below at *!&!* "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.? RL
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8652
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2782
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 we have to send another system

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.