wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes NullPointerException 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 » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Brian Smith
Ranch Hand

Joined: May 20, 2005
Posts: 63
I am trying to create an array of bean objects and keep getting a NullPointException during my "for loop". I may be going about this all wrong. When I step through the code the AccountBean constructor is completing but then when the value tries to get assigned to accountBeans[i], the code blows up. Thanks everyone.

ps, this code is to test some jdbc insert routines that the accountBean and accountBeans objects get fed into.

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In this line you declare accountBeans to be null.



You can't access a position in the accountBeans array until you create the array.
Brian Smith
Ranch Hand

Joined: May 20, 2005
Posts: 63
Ok, how should I declare the array? When i try this



I get an error that the variable accountBeans may not have been initialized.

Thanks for your help, I really appreciate it.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You want to include an initializer.

AccountBean[] accountBeans = new AccountBean[size];

where size is an int.

You must specify the size of the array when you create it.

An alternative is to use an initialization list, but I don't think that is appropriate for your case.
Brian Smith
Ranch Hand

Joined: May 20, 2005
Posts: 63
Thanks for the help, all works now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException