Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

adding Objects to its Array

 
Sam Nanda
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Report post to moderator
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];
 
Sam Nanda
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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!
 
    Bookmark Topic Watch Topic
  • New Topic