permaculture playing cards*
The moose likes Java in General and the fly likes adding Objects to its Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

adding Objects to its Array

Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
How can I add an Object to its Array??? When I create an instance of a Class and try to assign it to its array it gives me a Null Pointer Exceptions .... the line below in BOLD is where I get the exception.

Is this legal in Java??? if Yes, why am I getting a Null Pointer Exception, or is there something else I need to do ???



All suggestions are greatly accepted and thanked for.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
You have declared the array but never instantiated it with the new keyword.

You need to do something like this:

private Authorization authorizaition[] = new Authorization[42];


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Originally posted by Mark Van Tuyl:
You have declared the array but never instantiated it with the new keyword.

You need to do something like this:

private Authorization authorizaition[] = new Authorization[42];


Thanks Mark , it helps .... Appreciate it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This line:

private Authorization authorization[];

declares an array variable, but does not create the physical array object; the variable remains null. You have to actually create the array: for example,

private Authorization authorization[] = new Authorization[NUMBER_OF_AUTH];

Finally; please stop starting new threads regarding this single problem. Any followup should go in this original thread. Thanks for helping keep the Saloon neat and clean!


[Jess in Action][AskingGoodQuestions]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: adding Objects to its Array