HashMaps and Null values?

前端 未结 4 1657
闹比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:19

    Acording to your first code snipet seems ok, but I've got similar behavior caused by bad programing. Have you checked the "options" variable is not null before the put call?

    I'm using Struts2 (2.3.3) webapp and use a HashMap for displaying results. When is executed (in a class initialized by an Action class) :

    if(value != null) pdfMap.put("date",value.toString());
    else pdfMap.put("date","");
    

    Got this error:

    Struts Problem Report
    
    Struts has detected an unhandled exception:
    
    Messages:   
    File:   aoc/psisclient/samples/PDFValidation.java
    Line number:    155
    Stacktraces
    
    java.lang.NullPointerException
        aoc.psisclient.samples.PDFValidation.getRevisionsDetail(PDFValidation.java:155)
        aoc.action.signature.PDFUpload.execute(PDFUpload.java:66)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        ...
    

    Seems the NullPointerException points to the put method (Line number 155), but the problem was that de Map hasn't been initialized before. It compiled ok since the variable is out of the method that set the value.

提交回复
热议问题