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
beenerbeener 

Count() SOQL exception when no results

Hi,

 

I am attempting to query the number of rows I have for a particular object, so that if there are 0 objects, I don't operate.

 

I have tried the following line (this is the first line of code, which is called from an VisualForce ActionButton):

 

Integer Total_size = [select COUNT() from invoice__c where Week__c = :Current_Week_Ending.id];

I get the following error:

System.NullPointerException: Attempt to de-reference a null object

 
Any ideas why this happens? how can I verify that there are no objects before I work on it? for now, I will get the list and check it's size, but I think it's less efficient.

 

Thanks,

 

Ben 

 

Best Answer chosen by Admin (Salesforce Developers) 
uptime_andrewuptime_andrew
Is the Current_Week_Ending record defined?  Have you tried adding in some debugging to ensure that you aren't trying to reference the ID value of a null object?

All Answers

uptime_andrewuptime_andrew
Is the Current_Week_Ending record defined?  Have you tried adding in some debugging to ensure that you aren't trying to reference the ID value of a null object?
This was selected as the best answer
beenerbeener

Silly me.

 

Thaks.