473,651 Members | 2,743 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 1591
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
3744
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
3324
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
39838
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
5185
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 let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
19
3142
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
21
3198
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
104
16929
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 that array Could you show me a little example how to do this? Thanks.
7
6426
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 now thown out of the array released properly by the CLI?
2
2498
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 the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
2
2974
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
0
8803
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
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8581
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
7298
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
6158
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
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.