0

New to Java.

I'm trying to avoid NullPointerException when iterating over a list. CollectionUtils.emptyIfNull() helps, but not for a chained object.

For example:

for (MyObject myObj : CollectionUtils.emptyIfNull(
    result.item.getMyObject().getObjects())) {
    // do stuff
    }

I have no control over MyObject because I wrote a client that consumes this as a dependency. Any of these attributes could be null, and emptyIfNull() only protects against getObjects() returning null.

Is there a way to apply emptyIfNull() or something similar to the entire chain of calls?

Thanks for the help!

Bowser
  • 377
  • 2
  • 7
  • 16

0 Answers0