Home > OS >  How resolve null value in toMap
How resolve null value in toMap

Time:12-05

I have a problem, practically when I select two time slots within a week and generate them through a button all ok, but then when I go to delete a day of the week and then click again on the button "generate slots", I choose my week (where I had two days but now I have only one) I get the error on line 100 of code that I place there: enter image description here

CodePudding user response:

On the shared '100' line you basically telling to JDK compiler: for each object into list create a map entry with:

  • key the value of getDay from the object
  • value the object itself

The JDK converts it to:

  • key = object.getDay()
  • value = object

In your list there are items which are null, example: [{obj1}, {obj2}, null, ...].

This causes the TimeSlot::getDay to be null.getDay() in some cases. If you need to put null as key, you could transform it from TimeSlot::getDay to o -> o != null ? o.getDay() : null.

If you do not need null as key value, you could filter them before collecting, example adding: .filter(Objects::nonNull).

Check what-is-a-nullpointerexception-and-how-do-i-fix-it for more info.

  • Related