Page 1 of 1

enthuware.ocpjp.v7.2.1298

Posted: Thu Aug 24, 2017 5:06 am
by Anneke71
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

Re: enthuware.ocpjp.v7.2.1298

Posted: Thu Aug 24, 2017 12:03 pm
by admin
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.