null enum in Stream Api

Viewed 24

I have a Vehicle class with an enum(VehicleType) field. Objects of the Vehicle class are in the collection. I'm running through this collection through the steam api, sorting by Vehicle Type field, and writing this. But if VehicleType=null, I get a Nullpointerexception error.

    ConcurrentHashMap<Integer, Vehicle> concurrentHashMap=new ConcurrentHashMap<>();
    Vehicle vehicle=new Vehicle();
    Vehicle vehicle1=new Vehicle();
    vehicle.setType(null);
    vehicle1.setType(VehicleType.valueOf("PLANE"));
    concurrentHashMap.put(1,vehicle);
    concurrentHashMap.put(2,vehicle1);
    concurrentHashMap.keySet().stream()
            .sorted(Comparator.comparing(s->concurrentHashMap.get(s).getType()))
            .forEachOrdered(System.out::println);
0 Answers
Related