Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 30th, 2006, 10:09 AM   #1
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
creating an array of an object/class?

I can't seem to do it, I'm assuming its just like creating an array of anything else right?

So if I have one class:
[code]
import java.util.*;

public class abcd
{
public static void main(String [] args)
{
abc[] link = new abc[5];

link[0].number();
}
}

and the other one its supposed to create an array of:
import java.util.*;

public class abc
{
	Scanner keyboard = new Scanner(System.in);
	private int num;
	
	public void number()
	{
		num = keyboard.nextInt();
	}
	
	public int cda(int num)
	{	
		this.num = num;
		return num;										
	}
}
why do I get a null pointer exception at runtime?

It seems pretty straightforward to me...
chillypacman is offline   Reply With Quote
Old May 30th, 2006, 10:53 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 904
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You have to initialize each index of the array separately.
titaniumdecoy is offline   Reply With Quote
Old May 30th, 2006, 10:59 AM   #3
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
WHat you have done is created an array which is ready to store abc class objects. However, you need to assign objects to it before you can try calling functions (initialize each index as titaniumdecoy said). Storing objects in arrays does work the same as other things, strings for example. You would need to assign a string to each index before you called string methods, its the same idea.
NSchnarr is offline   Reply With Quote
Old May 30th, 2006, 11:06 AM   #4
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
lol, yeah I see now... I was on it for like an hour, I kept on going over and over it, I really didn't see anything wrong with it, except the most obvious thing...

but what if I initialise values within the class?

for instance if num = 1, wouldn't that mean its already initialised?

Just wondering...

oh and how do I intialize values for the array?
chillypacman is offline   Reply With Quote
Old May 30th, 2006, 11:30 AM   #5
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
You havent created an abc class object yet, so you would be unable to set num = 1, lol.
This is what you need to do.
abc obj1 = new abc();
abc obj2 = new abc();
link[0] = obj1;
link[1] = obj2;
this will at least store them for you, now as for working with then you will have to play around.
NSchnarr is offline   Reply With Quote
Old May 30th, 2006, 11:43 AM   #6
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
ok, I see.

but it does look like it will take a while, I mean I have to create a different object for every index in the array then I have to store them each in the array.

Couldn't I just skip the first part? I mean, isn't an array basically a collection of similar objects?
chillypacman is offline   Reply With Quote
Old May 30th, 2006, 1:13 PM   #7
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
no, an array is a place to store similar objects, you have to create them first.
NSchnarr is offline   Reply With Quote
Old May 31st, 2006, 6:39 AM   #8
azurik
Newbie
 
Join Date: May 2006
Posts: 6
Rep Power: 0 azurik is on a distinguished road
Quote:
Originally Posted by chillypacman
ok, I see.

but it does look like it will take a while, I mean I have to create a different object for every index in the array then I have to store them each in the array.

Couldn't I just skip the first part? I mean, isn't an array basically a collection of similar objects?
What you could do is:
final int ARRAY_LENGTH = 10;
abc[] array = new abc[ARRAY_LENGTH];
for (int arrayPos = 0; arrayPos < ARRAY_LENGTH; arrayPos++)
      array[arrayPos] = new abc();

That code will fill the array with initalized abc objects. If you ever need to call a method in the 4th array position, for instance, you would then do:

array[4-1].someMethod();

I'm not positive if it's conventional to do it that way, but I do it all the time anyway. :p
azurik is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC