HashMaps and Null values?

前端 未结 4 1659
闹比i
闹比i 2021-02-11 11:50

How do you pass in null values into a HashMap?
The following code snippet works with options filled in:

HashMap options = new HashMap         


        
4条回答
  •  爱一瞬间的悲伤
    2021-02-11 12:26

    It seems that you are trying to call a method with a Map parameter. So, to call with an empty person name the right approach should be

    HashMap options = new HashMap();
    options.put("name", null);  
    Person person = sample.searchPerson(options);
    

    Or you can do it like this

    HashMap options = new HashMap();
    Person person = sample.searchPerson(options);
    

    Using

    Person person = sample.searchPerson(null);
    

    Could get you a null pointer exception. It all depends on the implementation of searchPerson() method.

提交回复
热议问题