function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCDevQASFDCDevQA 

VF Error System Null Pointer Exception OR Compile Error: Non-void method might not return a value or might have statement after a return statement

I have a custom button which uses a controller class to clone an opportunity.  I'd like it to only allow the clone if certain criteria are met but I am running into an issue that if the criteria are not met I receive an error saying
"Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!cloneWithItems}' in component <apex:page> in page opportunity_clone: Class.OpportunityCloneWithItemsController.cloneWithItems: line 157, column 1"
that line is
return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
I tried adding in
  if(newopp.id != null){
      return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
        }else{
      ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The Opportunity could not be cloned');
      ApexPages.addMessage(msg);
But then I get "Compile Error: Non-void method might not return a value or might have statement after a return statement."
I understand that the reason I'm getting the first error is that if my criteria are NOT met then there will be no newOpp so it will be null, but I'm not clear on why it won't allow me to add a .message or if there is a way to just change the error that users see when they've tried to use the button incorrectly.  I'd prefer they just see a standard error saying "This Opportunity Cannot be Cloned" instead of the whole VF page error that references the class.

Thanks so much,
Amanda
 
Best Answer chosen by SFDCDevQA
NekosanNekosan
 if(newopp.id != null){
      return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
        }else{
      ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The Opportunity could not be cloned');
      ApexPages.addMessage(msg);
retur null;
}

you need to add return statements in all if/else. I hope this helps. 

All Answers

NekosanNekosan
 if(newopp.id != null){
      return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
        }else{
      ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The Opportunity could not be cloned');
      ApexPages.addMessage(msg);
retur null;
}

you need to add return statements in all if/else. I hope this helps. 
This was selected as the best answer
SFDCDevQASFDCDevQA
Thanks.  I actually did have the return null; statement in there. I just forgot to copy it with the rest.  Sorry about that.  I moved things around and reworded things a bunch of times.  Moved the if null part up to the very beginning of my class and then had the message part be this
return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
         }
         else{
         ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error: You cannot clone this Opportunity. Please contact a system Administrator for assistance');
         ApexPages.addMessage(msg);
        return null;
}

Also took out a Try Catch that maybe was interferring? 
Anyway it seems to be working now.  Thanks again for you help.  It at least encouraged me that I was doing something correctly and just needed to keep tweaking until it worked.