+ Start a Discussion
amritamrit 

System.TypeException: Invalid double

Hi,

 

Here is a trigger for generating automatic scheule setoff amount.Trigger is working fine.But when i run the test class it showing error like this  System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoSetOff_on_Receipt: execution of AfterInsert caused by: System.TypeException: Invalid double: null Trigger.AutoSetOff_on_Receipt: line 21, column 1: [].


How to resolve this error

 

      List<AggregateResult> groupedResults = [SELECT Sum(Amount__c)aver FROM Receipt__c
                                                Where Opportunity__c =: R.Opportunity__c
                                                AND Realization_Status__c = 'Done'];
       Double TotalReceipt = 0.0 ;
        if(groupedResults.size() > 0)
        {
            String str = '' + groupedResults[0].get('aver') ;
            TotalReceipt = Double.ValueOf(str) ;
            System.debug('TotalReceipt ::::: ' + TotalReceipt) ;
        }


Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi,

 

I think the value of "str" is not getting set properly.

What I would suggest is to put a System.debug() statement to print the value of "str" in your code (just below this line in your trigger)

 

String str = '' + groupedResults[0].get('aver') ; 

 

Then run your test class. Just download the debug log and check the value of str. This is the place where you are getting the issue. It may be because you have not set the test data properly.

 

Hope this will help you!!

All Answers

MandyKoolMandyKool

Just check the value of Double in your test method. Looks like it is getting set to NULL.

 

Also if possible paste the test class for reference and highlight the line that is causing this exception.

amritamrit

Thanks for your reply.This is the test class which i was testing.It is displaying me null value only

 

@isTest
private class ScheduleSetoff {
        static testMethod void myUnitTest1(){
        Test.startTest();
        Account a = new Account(name='test');
        insert a;
        Opportunity O = new Opportunity(
                            AccountId=a.Id,
                            Name='My Opportunity', 
                            StageName='Prospecting',
                            CloseDate=Date.today());
        insert O;
        Schedule__c sch=new Schedule__c(Name='On Booking',Opportunity__c=O.Id,Total__c=1200,Set_off_Amount__c=200);
        insert sch;
        Receipt__c rec=new Receipt__c(Name='rec123',Opportunity__c=O.Id,Amount__c=1000);
        insert rec;
        update O;
        Test.stopTest();
       }
      
      }

 

MandyKoolMandyKool

Hi,

 

I think the value of "str" is not getting set properly.

What I would suggest is to put a System.debug() statement to print the value of "str" in your code (just below this line in your trigger)

 

String str = '' + groupedResults[0].get('aver') ; 

 

Then run your test class. Just download the debug log and check the value of str. This is the place where you are getting the issue. It may be because you have not set the test data properly.

 

Hope this will help you!!

This was selected as the best answer
amritamrit

Thanks for your reply.It is working now.