Search code examples
javareturnunreachable-statement

Java "add return statement" error


public class1 foo ( class1 t)
{
    if ( object == null ) return t;
    else foo(t.childObject);
}

Java keeps telling me that there is no return statement. I can understand what is wrong here, but I cannot fix it without removing the recursion, which I really need. Is there any way to bypass this error?


Solution

  • You need a return in the else case.

    public class1 foo ( class1 t)
    {
        if ( object == null ) return t;
        else return foo(t.childObject);
    }