Results 1 to 8 of 8
- 10-19-2009, 12:44 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
How to create an array of objects
Hi Guys,
This is the problem I have
Partier lowie= new Partier(Buff, full, empty, mutex);
Partier duey= new Partier(Buff, full, empty, mutex);
Partier huey = new Partier(Buff, full, empty, mutex);
if I run lowie.start();
duey.start();
huey.start();
then I have no issues....
but I want to create a list of arrays such as lowie[0] lowie[1] lowie[2]
so that I can execute lowie[0].start();
lowie[1].start();
lowie[2].start();
how to declare an array of objects?
I know that I can't by declaring Partier lowie[] = new Partier[2];
thanks in advance.....I am not strong in the fundamentals..
-
Please have a look here: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
It's the best way to get stronger on the fundamentals.
Luck.
- 10-19-2009, 01:33 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
thanks for the link.....but it doesn't say how to solve my problem of creating an array of objects
-
Well, you create an array just as you said not to:
I know that I can't by declaring Partier lowie[] = new Partier[2];
- 10-19-2009, 01:51 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
I am getting this error
when I tried declaring it that way.Java Code:Exception in thread "main" java.lang.NullPointerException at Pledge_Party.main(Pledge_Party.java:153)
-
That's probably not due to declaring it like this but is likely because you haven't initialized the array items before using them.
Let's see your code. That'll probably clear this up.
- 10-19-2009, 02:03 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
I tried declaring it this way after picking up this code from a website
I am able to run the program without any problems....Java Code:for(int i=0;i<4;i++) { rer[i] = new Partier(Buff, full, empty, mutex); }
thanks!
-
Cool. Glad you have it working. Just one little bit of advice: if possible, let the array itself dictate the upper bound of the for loop:
This will help prevent array index out of bounds errors.Java Code:for (int i = 0; i < rer.length; i++) { rer[i] = new Partier(Buff, full, empty, mutex); }
Similar Threads
-
Array of Objects
By sfe23 in forum New To JavaReplies: 19Last Post: 02-04-2009, 05:57 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM -
array of objects
By Jack in forum New To JavaReplies: 2Last Post: 07-02-2007, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks