I am getting NullPointerException in the following code :
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);
}
}
I think its because of
List<Entry> entries = null;
but then how do I create an empty list and add it to a map ?
You must initialize your map:
Note that you can use
and add to the map, instead of adding a null.
About NullPointerException
Thrown when an application attempts to use null in a case where an object is required. These include:
Since you did not initialize you Map object when you did this:
you get NullPointerException because you are “Accessing or modifying the field of a null object.”.