+ Start a Discussion
Sohail Barat 14Sohail Barat 14 

Non-void method might not return a value or might have statement after a return statement.

I am getting this error in my code:
Non-void method might not return a value or might have statement after a return statement.
My code is as under:
public class AccountHandler {
    public static Account insertNewAccount(String AcctName)
    {
        Account acct = new Account();
        acct.Name = AcctName;
        if(acct.Name == null)
        {
            return null;
        }
        else
        {
               return acct;
        }
     }
}
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi Sohail,

Make sure you have return statement outside if else as well.

Thanks,
N.J
Sohail Barat 14Sohail Barat 14
Why is it necessary ?? I have wrote return statements based on the logic.. So, why one extra return statement ??
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Even I dont see any obvious reason to have return since you already have it in both conditons. Also, I am able to save you class in my dev org, for any API version.
Sohail Barat 14Sohail Barat 14
because I have to return sObject 
bouscalbouscal
I just copy/pasted your code above into a new class in my dev org and it saved.  No error messages at all.
I can duplicate your error by commenting out either of the return lines.  I'd double check your code, likely something missing in translation from what you have to what you've posted here.
 
public class AccountHandler {
    public static Account insertNewAccount(String AcctName)
    {
        Account acct = new Account();
        acct.Name = AcctName;
        if(acct.Name == null)
        {
            return null;
        }
        else
        {
            return acct;
        }
     }
}