enthuware.ocpjp.v7.2.1298

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Anneke71
Posts: 2
Joined: Thu Aug 24, 2017 3:34 am
Contact:

enthuware.ocpjp.v7.2.1298

Post 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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocpjp.v7.2.1298

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 28 guests