How to return nothing from a function that returns value?

Your function does not have a return for every possible circumstance. You have:

if (null)...

if (less than root)...
else ( if ...)
       else (no return!)

What do you return if it is not null, and goes to the final else? Nothing is returned.

You can either return getParent... in the else statement. or return null at the end of the function (not in an if or else statement)

I often see code like so to cover the event of neither if statement returning a value.

public int get()
{
    if (answer.equals("yes"))
        return 0;
    else if (answer.equals("no"))
        return 1;

    return null;
}

Leave a Comment