Java 将空列表添加到地图

Java 将空列表添加到地图,java,Java,我在以下代码中获得NullPointerException: private Map<String,List<Entry>> Days; private void intializeDays() { //Iterate over the DayOfWeek enum and put the keys in Map for(DayOfWeek dw : EnumSet.range(DayOfWeek.MONDAY,DayOfWeek.SUNDAY)){

我在以下代码中获得NullPointerException:

private Map<String,List<Entry>> Days;
private void intializeDays() {
    //Iterate over the DayOfWeek enum and put the keys in Map
    for(DayOfWeek dw : EnumSet.range(DayOfWeek.MONDAY,DayOfWeek.SUNDAY)){
    List<Entry> entries = null;
    Days.put(dw.toString().toLowerCase(),entries);
    }
}
私人地图日;
私有无效初始值(){
//迭代DayOfWeek枚举并将键放入映射中
for(DayOfWeek dw:EnumSet.range(DayOfWeek.MONDAY,DayOfWeek.SUNDAY)){
列表条目=空;
Days.put(dw.toString().toLowerCase(),条目);
}
}
我想是因为

List<Entry> entries = null;
列表条目=null;

但是,如何创建空列表并将其添加到地图中?

您必须初始化地图:

private Map<String,List<Entry>> Days = new HashMap<>();
由于执行此操作时未初始化映射对象:

Days.put(dw.toString().toLowerCase(),entries);
您将获得NullPointerException,因为您正在“访问或修改null对象的字段”

或者用另一种方式初始化它

作为状态,
null
键和值在
HashMap


还要注意的是,代码中没有空列表,根本没有列表。

问题在于天数映射。试试this Days=new HashMap();
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
Days.put(dw.toString().toLowerCase(),entries);
private Map<String,List<Entry>> Days;
private Map<String,List<Entry>> Days = new HashMap<>();