Search code examples
javanullpointerexceptionguava

TryFind method in Guava throw NullPointerException


I get a NullPointerException while running my webapplication, I save a logs and start server again and this error no longer there appeared, but i want protect method to NullPointer never appeared. I sure this line is responsible for this error

Vehicle pVehicle =
        Iterables.tryFind(aFactory.getVehicle(), new Predicate<Vehicle>() {
            @Override
            public boolean apply(Vehicle vehicle) {
                return vehicle.getVehicleBrand().equals(aVehicle.getVehicleBrand());
            }
        }).orNull();

But this is not my code, and i don't known, which part may cause this error> Please, which element can be appeared for this NullPointer and how protect this code


Solution

  • Check the following statements:

    • aFactory - Because of the getVehicle() call.
    • aFactory.getVehicle() - Because Iterables.tryFind expects a non-null Iterable
    • vehicle - Because of the getVehicleBrand() call
    • aVehicle - Because of the getVehicleBrand() call
    • vehicle.getVehicleBrand() - Because of the .equals() call

    If any of these were null it would cause an NullPointerException