about array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: about array

  1. #1
    Join Date
    Aug 2005
    Posts
    9

    about array

    ok i have created a Customer class with get and set methods... (compiled ok)
    i have also created another file which main is invoked...
    in main i invoke a static method which i defined below the main method...
    in this static method... i want to create an array of Customer...
    so i did this...

    Customer[] cusArr = new Customer[100];

    but when i set it using

    cusArr[0].setName("customer name");

    it gave me a runtime error of nullpointerException at the above line...
    what is wrong...? it compiled ok....

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    with the statement Customer[] cusArr = new Customer[100]; you declared an array of Customer object. However, you still need to declare each object in the array seperately.
    cusArr[0] = new Customer();
    cusArr[0].setName("customer name");

  3. #3
    Join Date
    Aug 2005
    Posts
    9
    ok thks... figured that out... so the "new Customer[100]" line doesn't instantiate a instance of 100 Customer class... it just merely declaration eh..?

  4. #4
    Join Date
    Oct 2004
    Posts
    311
    Yeah, it instantiates the Array
    But an Array is nothing more then a list of pointers. You will next have to tell the JVM where the pointers are pointing to. This is done with the actual instantiation of the new Customer();

    Keeping this in mind, it might be a good idea to overload the constructor of the Customer class and create on e containing all the variables you wish to put in there.
    Continuing your example, this would mean setting up the following constructor:

    public Customer(String name)
    {
    setName(name);
    }

    This would enable you to instantiate and set the customer name in one method call:

    casArr[0]=new Customer("customer name");

Similar Threads

  1. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  2. Replies: 3
    Last Post: 04-08-2005, 05:49 AM
  3. Accessing jagged array values
    By Jason Salas in forum ASP.NET
    Replies: 2
    Last Post: 04-20-2003, 11:39 PM
  4. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 10
    Last Post: 06-16-2000, 05:34 AM
  5. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 0
    Last Post: 06-12-2000, 08:15 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center