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
gvgv 

Getting 'Non-Void method might not return a value' error

I have the following function call :

 

List<Contract__c> lstContracts = QueryContractRequests(); 

 

The QueryContractRequests is defined as:

 

private List<contract__c> QueryContractRequests(){

 

 return[select contract_id__c,contract_name__c,contract_class__c

    from contract__c

    where status__c = 'New'

and opportunity__c =  :this.currentOpportunity.Id

 

 

I kept getting the 'Attempt to de-reference a null object ' error in the line 'and opportunity__c =  :this.currentOpportunity.Id'.

So I thought checking for null before returning the value would be a better option and changed the method as below

 

private List<contract__c> QueryContractRequests(){

 

 

id opportunityid;

   opportunityid = this.currentOpportunity.Id;

    

   if (opportunityid <> null){ 

 return[select contract_id__c,contract_name__c,contract_class__c

    from contract__c

    where status__c = 'New'

and opportunity__c =  :this.currentOpportunity.Id

 

}} 

 

But now I keep getting the ''Non-Void method might not return a value' error in this line 'if (opportunityid <> null){ '

 

Where am I going wrong?

 

Thanks in Advance

 

AkiTAkiT
You should add else statement and at least return null or something, so your non void method is correct.
gvgv

Thanks for the reply.

 

But if I reply null in the else, would not that be a problem?

 

 

prageethprageeth

Returning a null is not a problem if you handle it at the place where you call QuerycontactRequests() method.

For an example: 

 

if (QuerycontactRequests() != null) {

//youe code here

}

 

 

Or you can return a new List of contract__c Objects instead of returning a null value.

 

return new List<Contract__c>();

 

This returns an empty list of contract__c Objects.