I have a nested object which can return a null at any point of time.
Thanks to Optional and map we can now do nested calls without having to put null checks after every get.
I have a very unique requirement where I need to know at which step exactly did I encounter a null object for e.g. (Copied from StackOverflow)
Optional.of(new Outer())
  .map(Outer::getNested)
  .map(Nested::getInner)
  .map(Inner::getFoo)
  .ifPresent(System.out::println);
How can I LOG a different kind of log message depending on when and where I encounter a null value?
The code below is not valid but I am just trying to explain how it might look like programmatically:
Optional.of(outerObject).else(LOG.error("Outer was null"))
  .map(Outer::getNested).else(LOG.error("Nested was null"))
  .map(Nested::getInner).else(LOG.error("Inner was null"))
  .map(Inner::getFoo).else(LOG.error("Foo was null"))
  .ifPresent(System.out::println);
 
     
     
    