Why does the second line System.out.println(bs.getNumberOfCopies(b)); get a NullPointException since the second book b has not been added to the BookStore and the HashMap.
So I thought the value of the first (and only) Book b was caught from the BookStore
enthuware.ocpjp.v7.2.1298
Moderator: admin
-
- Posts: 2
- Joined: Thu Aug 24, 2017 3:34 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1298
When you call bs.getNumberOfCopies(b) using the second Book object, the call to map.get(b) inside getNumberOfCopies method will return null (because the second book object has a different hashcode from the first one). Since the return type of getNumberOfCopies method is int, the jvm will try to convert null into an int, which will cause a NPE to be thrown.
Who is online
Users browsing this forum: No registered users and 17 guests