GeeCON Prague 2014*
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
Author

Array Initialization

Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi,
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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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
Hi,
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
Sheriff

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.
Anurag
 
GeeCON Prague 2014
 
subject: Array Initialization