Results 1 to 6 of 6
- 02-24-2009, 04:43 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
Need help with a simple Java thing involving array of objects
So I have 3 classes. I need to have 10 circles with random dimensions and placement. I'm stuck at an early stage of writing this... I have a feeling it's because of toString? I don't understand how to use it. But whenever I run this the array is filled with all nulls.
*code removed by me*
Please help me! I've been stuck on this one basic thing for a while. After I figure it out I think it'll be easy to finish the rest of this. What am I doing wrong?Last edited by Jeremy8; 04-22-2009 at 08:00 PM.
-
What if you change this:
to this:Java Code:allInformation += collection1[circle] + "\n";
The reason being is that you are adding Circle objects to your allInformation String, and I don't think you want to do that. I think that you want each Circle's toString() output to be added to the allInfo string, and so you'll have to specifically request this in your code.Java Code:allInformation += collection1[circle].toString() + "\n";
- 02-24-2009, 06:47 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
Yah I tried that and it compiles but then I get this error message when I run it:
I don't know what that meansjava.lang.NullPointerException
at Collection.printAll(Collection.java:29)
at Circles.main(Circles.java:14)
But you're right about what I'm trying to do.
-
The NPE means that you're trying to use objects that have not yet been initialized. Please note that when you create and initialize an array of objects, you just initialize the array, not the objects it contains. You have to do that separately by looping through the array and initializing each object. Consider doing this in your Collection constructor.
- 02-24-2009, 10:11 PM #5
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
This is excellent code.
It is about 90% working.
It needs a few details.
The first detail is that it needs
a method that fills the collection
with 'new Circles' (hint).
I made such a method and called it,
"public void fillWithRandomCircles()",
and placed it in the Collection class.
REGARDING "toString()":
It is being used entirely correctly
in this code.
"toString()" is a method ALL CLASSES
have in Java -- even if you make up
your own class, "toString()" will be
there along with eight other methods
you can use, but don't know about yet.
To find these methods, along with
toString(), click on
"java.lang.Object"
anywhere in the Java API.
YOUR CODE overides toString(). To
see how toString normally behaves,
comment out your toString() code,
re-compile, then run it and see.
- 02-25-2009, 07:14 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Simple program involving military time
By busdude in forum New To JavaReplies: 4Last Post: 10-08-2008, 06:03 PM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
PLz i really need help on this final thing
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:31 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