• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

NullPointer when add to HashMap

 
Ranch Hand
Posts: 69
  • 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!!
 
author & internet detective
Posts: 39433
768
Eclipse IDE VI Editor Java
  • 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: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kristina Hawkins
Ranch Hand
Posts: 69
  • 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
Posts: 39433
768
Eclipse IDE VI Editor Java
  • 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.
 
Marshal
Posts: 65379
248
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!