If I have a hierarchy situation like this:
class foo1{
Foo2 foo2;
}
class foo2 {
List<Foo3> foo3;
}
class foo3 {
}
class foo4 extends foo3 {
Foo5 foo;
}
class foo5 {
double value;
}
I want to get that final double value
but to get there I have to go down the hierarchy and check for all the nulls. I could do something like this:
if(foo1 != null) {
if(foo.foo2 != null) {
if(foo.foo2.foo3 != null) {
if( ((foo4) foo.foo2.foo3).getFoo5() != null) {
if(((foo4) foo.foo2.foo3).getFoo5().getValue() != null) {
//do something
}
}
}
}
}
But that looks very ugly and there's probably some much easier and cleaner way to achieve the same goal. I've come across using reflection but I'm not really sure how I would go about using that in the above way. Any ideas how to do that without throwing an NPE?