Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Initialization

 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for ur reply.
Anurag
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic