473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array of objects

77 New Member
Hi ppl,
whenever we write
Expand|Select|Wrap|Line Numbers
  1. class object=new class();
object gets created; assigning values to it is perfectly fine.
now in case of array objects:
Expand|Select|Wrap|Line Numbers
  1. class object[]=new class[3];
i had assigned values to
Expand|Select|Wrap|Line Numbers
  1. object[0]...3
by
Expand|Select|Wrap|Line Numbers
  1. object[0].variable
..its syntactically right but giving run-time error of null pointer assignment.
Whenever we create an object of any class its a reference variable untill memory is assigned to it..
but we are clearly using new operator and allocating memory to each object in object array.....where am i mistaking?
Jan 25 '10 #1
4 1586
Curtis Rutland
3,256 Recognized Expert Specialist
I could be wrong, but if Java is like some of the other OOP languages I worked with, when you do this:
Expand|Select|Wrap|Line Numbers
  1. class object[] = new class[3];
You are actually creating a new array, and allocating the memory space for three pointers, but you are not initializing any of those three new objects.

You still need to initialize each object in the array before you can use them:
Expand|Select|Wrap|Line Numbers
  1. object[0] = new class();
Or as a loop:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<3; i++)
  2.   object[i] = new class();
I hope that is correct and helps.
Jan 25 '10 #2
Time
77 New Member
Yeah i agree with you; that they are not initialized; but
object[0]=new class();
allocates memory as new is used and not just initialization..
Jan 26 '10 #3
Curtis Rutland
3,256 Recognized Expert Specialist
I'm afraid I don't understand your question here.
but we are clearly using new operator and allocating memory to each object in object array
That's not quite true. You're just allocation the memory for a pointer. Those pointers are null. You have to initialize each object in the array if you want to use them.

If that's not what you are asking, please clarify your question.
Jan 26 '10 #4
pbrockway2
151 Recognized Expert New Member
@Time
The line of quote you quoted comes from insertAlias and that does create a new (class) object.

Your code was "class object[]=new class[3];" and what that does is create a new array.

Think of the array as a box - with little slots in it marked zero to two. Creating a box (an empty one) is one thing. Creating objects to put in the box is quite another.
Jan 26 '10 #5

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

Similar topics

4
3709
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
2
3310
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
7
39827
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
5145
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
19
3127
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
21
3168
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
16851
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
6409
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
2480
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
2
2962
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
7199
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,...
0
7274
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,...
0
7453
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
5576
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
3162
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
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
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
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
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.