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
LuvaraLuvara 

Null object in aggregate result

I am trying to display an aggregate result of various metrics in visualforce pages. The result displays fine in the pages, but I am getting the error "Attempt to de-reference a null object" when I run the tests.

 

    public decimal getCaseSLA (){
        AggregateResult agg2 = [Select AVG(SLA_Response_Time__c) sla from Case
                                WHERE CreatedDate = LAST_N_DAYS:30
                                ];
                                
    decimal cs3 = (decimal)agg2.get('sla'); 
    return cs3.setscale(2);
    }

 I've tried a few iterations with no luck. Help would be appreciated.

 

Thanks,

 

Chris

 

Eli Flores, SFDC DevEli Flores, SFDC Dev

What's the test code?

 

It seems like either you have to add some test data so it's guaranteed to be not  null.

 

Or you can amend you method with an if

 

decimal cs3 = 0.0;
if(agg2!=null) {
   cs3 = (decimal)agg2.get('sla'); 
} 

 

LuvaraLuvara

I was adding in my own test data, but as soon as cleared that out, and went with the following, it's now working. Always something simple...

 

private static testMethod void clsMonitorMetrics () {
    
            clsMonitorMetrics  mm = new clsMonitorMetrics ();
            
            Test.setCurrentPageReference(new PageReference('Page.MetricsScore'));
            clsMonitorMetrics  controller = new clsMonitorMetrics ();
            Integer i1 = controller.getOpenCases();
            Integer i2 = controller.getCasesLast30();
            Integer i3 = controller.getCaseTrend();   
            Decimal i4 = controller.getCaseScoreLast30();
            Decimal i5 = controller.getCaseT2C30();
            Decimal i6 = controller.getCaseSLA();           
                            
            mm.getCseSvy();        
        }