Avoiding null checks in java

Hi,
I have the following code :
if (task.getCompletor() != null) {
                task1.setCompletorGuid(task.getCompletor().getUserGuid());
            }

The getCompletor returns a User -  :
public WorkflowUser getCompletor() {
        return completor;
    }

what are the ways i could avoid the null check.
One way i can think of to user a NullWorkflowUser class so instead of directly returning the completor from the getter i return this object in case its null this way i dont have to have a null check and i could write the definitions which will be null for the methods inside it.
I have read about Optionals but never used it.
Can that be used here.
Which is a better approach the NullWorkflowUser or the Optional one.

And any other alternatives in java.
Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
1) If you want to use a NullWorkflowUser you will still need to check for null values, otherwise how do you know how to instantiate a NullWorkflowUser object instead of the WorkflowUser?

2) You can use Optional but you will need to wrap everything in an Optional class, e.g. Optional<WorkflowUser>. But this is a clearer solution since with optional you have several advantages over a null value:
a) you explicitly express that the value is optional
b) you clarify that optional is a perfectly valid value and that an optional value is not an application error (for example if you get a null value you cannot know if this is an application error, e.g. you forgot to initialise the value, or a business requirement, the object can indeed be null)

Of course there are disadvantages to using Optional, namely that they are not serialisable (if you need serialisation, although there are workarounds) and that you have to wrap up everything to an Optional.

3) There is another way, to use assert, but assert will throw an error if the condition is false, so I do not think this is what you're looking for.

If I had to choose from all the points above, and you absolutely need to remove all null checks, I would go with the Optional.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.