-
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....
-
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");
-
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..?
-
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
-
By kanakatam in forum Java
Replies: 2
Last Post: 04-15-2005, 09:06 PM
-
By Shaitan00 in forum Java
Replies: 3
Last Post: 04-08-2005, 05:49 AM
-
By Jason Salas in forum ASP.NET
Replies: 2
Last Post: 04-20-2003, 11:39 PM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 10
Last Post: 06-16-2000, 05:34 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks