Results 1 to 2 of 2
  1. #1
    ravi1912 is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default array of class objects

    Hey i want to know how to create array of object in java

    but i always got an error NullPointerException


    plz suggest a better example

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,316
    Blog Entries
    7
    Rep Power
    25

    Default

    Quote Originally Posted by ravi1912 View Post
    Hey i want to know how to create array of object in java

    but i always got an error NullPointerException


    plz suggest a better example
    Declaring an array of objects doesn't automagically give you one in Java; creating an array of objects doesn't automagically populate it with objects automagically in Java; you have to do that yourself, e.g.

    Java Code:
    MyClass[] array; // declare an array
    array= new MyClass[42]; // create it
    array[0]= new MyClass( ... );
    array[1]= new MyClass( ... ); // populate it
    Java allows a handy shorthand form for the above:

    Java Code:
    MyClass[] array= { new MyClass( ... ), new MyClass( ... ) };
    This form defines, creates and populates an array that is able to store the elements mentioned in the (so called) initializer list.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 06:49 AM
  3. help with and array of objects
    By hoosierfan24 in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 03:45 AM
  4. Replies: 1
    Last Post: 01-03-2010, 10:48 PM
  5. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 06:57 PM

Posting Permissions

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