• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointer when add to HashMap  RSS feed

 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I don't know why it throws a NullPointer:



BankCustomer:



Account class:



Any Help will be greatly appreciated!!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina,
We'd need to see more code to know for sure, but it looks like newCustomer.getMyAccount() is null. You check that newCustomer itself isn't null. But not that its account isn't null. So then getting the account number has the possibility of a NullPointer.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I just realized that newCustomer.getMyAccount() isn't initialize, but not sure why it wasn't given by instructor.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the instructor expects you to either call setMyAccount() or have a null check for it.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot expect an instructor to do everything for you.
I believe that you shou‍ld write a constructor for every class you write. And that every constructor shou‍ld initialise every instance field to a sensible value. Having sensible value for all instance fields is called having your object in a consistent state.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!