I have a Class structure such as
Class A {
int val;
}
Class B {
A a;
}
Class C {
B b;
}
Now, all the classes are from a third party service, from which I get a response and I want to read the int value. But i dont want to do something like
lets assume c is a variable of type C.
if (c != null && c.getb != null) {
B b = c.getb()
and so on.
}
I want to avoid these null checks as in reality these pojos are very hierarchical and have a lot of fields in them.
I tried using
Optional<Integer> val = Optional.ofNullable(c.getb().geta().getval());
val.elseThrow(Ex::new)
but this is not the right approach as (it throws null pointer exception if b is not present as optional is on int). What can i do to tackle such a situation where i don't have control over the declarations of Pojos but want to avoid bull checks?