Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Avoiding null checks in java

Avatar of Rohit Bajaj
Rohit BajajFlag for India asked on
Java
1 Comment1 Solution107 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Avatar of girionis
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers