java8/Java8InAction

Chapter 10 examples,I got error!

antianchi opened this issue · 1 comments

Can you run?I can not successfully run the OptionalMain.java.What went wrong,Here
public String getCarInsuranceName(Optional<Person> person) { return person.flatMap(Person::getCar) .flatMap(Car::getInsurance) .map(Insurance::getName) .orElse("Unknown"); }

I got NullPointException.The Exception:
image

maybe you can modify your Person.java like this

public class Person {

    private Car car;

    public Optional<Car> getCar() {
        return Optional.ofNullable(car);
    }

}