Exception when working with Map collections

  • 3
  • Locked
We have some questionable code. It's time to deploy an agent to intercept (catch) exceptions. Here's the code: HashMap<String, String> map = new HashMap<String, String>(null); map.put(null, null); map.remove(null); Agent, your task, as usual, is to catch the exception and display it on the screen. Don't forget to only display the exception type.
You can't complete this task, because you're not signed in.
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Michael
Level 10 , Dresden, Germany
28 August 2021, 08:15
Hello Kent, concerning the null value in a HashMap I found this on stackoverflow (see: HashMap with null as a key) "HashMap does not call hashcode when null is passed." As I understand this statement, you cannot remove something from a null value (even if it is 'null' which you want to remove) in a HashMap and therefore an error is created.
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
6 February 2020, 19:58
I don't know what this does: HashMap<String, String> map = new HashMap<String, String>(null); I thought this was a clue: HashMap public HashMap(int initialCapacity) Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75). Parameters: initialCapacity - the initial capacity. Throws: IllegalArgumentException - if the initial capacity is negative.
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
6 February 2020, 19:59
but running it showed the exception was different I still don't know what the null does, but I have passed this task