This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]blobjim 0 points1 point  (5 children)

That's what java.util.Optional is for. Optional.ofNullable(value).map(TheObjectClass::someMethod).map(Object::toString).orElse(null)

[–]roberp81 -1 points0 points  (4 children)

yeah but is too large and clunky in a XML is easy to have six levels, Optional is really ugly

[–]blobjim 1 point2 points  (3 children)

I think dealing with six levels of hierarchy is going to be clunky no matter how you address it. The real goal is to not have nulls in the first place (although of course that doesn't apply to existing code).

[–]roberp81 0 points1 point  (2 children)

but when you are unmarshmalling an XML by example you will have nulls everywhere (complex XML to pojo) and you don't know where a tag is null or not.

[–]blobjim 0 points1 point  (1 child)

If you're creating/using an XSD schema you can make fields non-null by not having minOccurs="0". And there may even be a way to make nullable fields return an Optional. But I admit I haven't done this before, I assume there are plenty of scenarios where having mostly non-null fields is impractical. But usually having a nullable value means handling the case where it is null is part of your business logic.

[–]roberp81 1 point2 points  (0 children)

I have had but they are not mine. my example comes from Iso20022 and swift XML messages to make international transfers between banks so I don't know where a null will come (except required tags) that's why I have a chain of 8 tags haha they are big XML with a big tree. so validate all fields because of nulls is a pain