jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Array Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Initialization" Watch "Array Initialization" New topic

Array Initialization

Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
here i Have written a class "vObject"which has two public fields
Name and Salary.
In another class i m creating an Array object of this class,, and trying to set values but at runtime its giving Null pointer Exception can anyone tell me why its like that or whats the solution.

Here in second class on runtime its throwing Null Pointer Exception??
[ edited to help preserve formatting of code with the [code and ]/code UBB tags -ds ]
[ December 28, 2003: Message edited by: Dirk Schreckmann ]

SCJP 1.2
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Creating an Object array does not automatically populate that array with objects; each element in the array is initialized to null.
Whe you need to do, then is:

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
thats fine but it won't be efficient to create new Object each time when i
want to assgn some value.
Is there some other way by whihc i can do same.
thanks in advance.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Actually that's the only way to do this. You can't use an object that doesn't exist, so the object must be created.
Why are you concerned about efficiency? According to some of the JVM writers at JavaOne, object creation has been designed to be very efficient and fast in Java.
Donald Knuth is quoted as saying something akin to "Premature optimization is the root of all evil." I'd suggest that your concern for efficiency is ill-placed.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
thanks a lot for ur reply.
I agree. Here's the link: http://aspose.com/file-tools
subject: Array Initialization
It's not a secret anymore!