Results 1 to 8 of 8
  1. #1
    redmaverick is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

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

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

  3. #3
    redmaverick is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks for the link.....but it doesn't say how to solve my problem of creating an array of objects

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

    Default

    Well, you create an array just as you said not to:
    I know that I can't by declaring Partier lowie[] = new Partier[2];

  5. #5
    redmaverick is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    I am getting this error
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at Pledge_Party.main(Pledge_Party.java:153)
    when I tried declaring it that way.

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

    Default

    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.

  7. #7
    redmaverick is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    I tried declaring it this way after picking up this code from a website

    Java Code:
    for(int i=0;i<4;i++)
               {
                 rer[i] = new Partier(Buff, full, empty, mutex);
               }
    I am able to run the program without any problems....

    thanks!

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

    Default

    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:
    Java Code:
    for (int i = 0; i < rer.length; i++) 
    {
      rer[i] = new Partier(Buff, full, empty, mutex);
    }
    This will help prevent array index out of bounds errors.

Similar Threads

  1. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 06:57 PM
  2. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 05:03 AM
  4. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 10:50 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 06: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
  •