-2

Trying to create filter, need to choose records which type is not equals "N". I trying do it by this way, but I getting NullPointer. I think this is because in my database Type sometimes is null. How can I fix this filter to not get NullPointer?

Collection<Plan> plan = packet.getPlan().stream() //    
            .filter(item -> !item.getType().equals("N")) //
            .map(packetPlan -> Plan.newInstance( //
                    packetPlan, //
                    activePlan.contains(packetPlan.getType()))) //
            .collect(Collectors.toList());
Deividas.r
  • 17
  • 1
  • 4
    "_I think this is because in my database Type sometimes is null._" - correct. "_How can I fix this filter to not get NullPointer_" - add a check for `null` in your `filter()` before getting the type. – maloomeister Sep 13 '21 at 08:07
  • 1
    did you try check if object is not null before getting the type? – Timor Sep 13 '21 at 08:08
  • What if I need to select those records, which have `Type` null or not equal "N"? Then adding this check is not good for me. – Deividas.r Sep 13 '21 at 08:13

1 Answers1

1

Just add another filter

Collection<Plan> plan = packet.getPlan().stream() //    
            .filter(item -> item != null) //
            .filter(item -> !item.getType().equals("N")) //
            .map(packetPlan -> Plan.newInstance( //
                    packetPlan, //
                    activePlan.contains(packetPlan.getType()))) //
            .collect(Collectors.toList())
Antoniossss
  • 31,590
  • 6
  • 57
  • 99