• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException from calling a setter in an array of Objects  RSS feed

 
Frank Serkland
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm putting this question in the right folder. I have an array of objects, and I have defined a setter for a variable in the object. When I call the setter, I get a NullPointerException. Here is the relevant code for the object.



Here is the portion of the main class where I define an array and then call the setter.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frank,

Since this is a learning site, let me ask why do you think the NullPointerException is being thrown?
If you are not sure, why don't you see what the output for the following code is:

Now, why do you think that is?
What do you know about the initialized default value of an array component and what do we need to do to fix it?

Cheers!
Chris
 
Frank Serkland
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding this as the first line in the for loop cured the problem.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out, but have you worked out why the exception occurred? CRB's post should make that easier.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of objects


Do you? Or do you have an array of references that point to objects? Answer that and it should help with the questions they're asking above! ^_^
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!