Results 1 to 6 of 6
  1. #1
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What if you change this:
    Java Code:
    allInformation += collection1[circle] + "\n";
    to this:
    Java Code:
    allInformation += collection1[circle].toString() + "\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.

  3. #3
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    Yah I tried that and it compiles but then I get this error message when I run it:

    java.lang.NullPointerException
    at Collection.printAll(Collection.java:29)
    at Circles.main(Circles.java:14)
    I don't know what that means

    But you're right about what I'm trying to do.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    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.

  6. #6
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    Thanks guys! I think it's working now! I'll let you know if I run into any other issues.

Similar Threads

  1. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  2. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 06:03 PM
  3. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  4. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •