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

NullPointerException

 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, all works now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic