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
NathanW.NathanW. 

Yet another 'de-reference a null object post'

I am getting the annoying "Attempt to de-reference a null obect" error when trying to deploy a VF page and Class/Test Class. I have 100% code coverage on my test class and it functions perfectly in sandbox. I cannot seem to figure out where/why I am getting the error. Below is the code for both the class and test class. I am getting an error at line 64, 96, and 127 in the class. 
public class ctrl_qliEstimatedSellFor {
    
    public list< QuoteLineItems__c > qli_List	{get;set;}
    public List < SystemsOrOptions__c > soo_List {get;set;}
    private final Quotes__c qte_sObj;
    decimal pem {get;set;}
    decimal pf {get;set;}
    decimal se {get;set;}
    decimal GMNC {get;set;}
    decimal CostTotal {get;set;}
    decimal TravelCost {get;set;}
    decimal TravelCostTest {get;set;}
    decimal CommissionPercentage {get;set;}
    decimal SellForWoCommission {get;set;}
    decimal CommissionAmount {get;set;}
    decimal EstimatedSellForTotal {get;set;}
    
    public ctrl_qliEstimatedSellFor( ApexPages.StandardController stdController ){
        
        this.qte_sObj = ( Quotes__c )stdController.getRecord();
        
        qli_List = new list< QuoteLineItems__c >();
        soo_List = new List <SystemsOrOptions__c > ();
        TravelCost = 0;
        TravelCostTest = 0;
        EstimatedSellForTotal = 0;
        
        for( QuoteLineItems__c q : [SELECT Id,Name, RelatedQuote__r.CommissionPercentageForCalculations__c, RelatedQuote__r.FinalCommissionAmount__c, Qty__c, FlowItemNumber__c, RelatedQuote__r.SellForPricewoCommission__c, RelatedQuote__c, 
                                    Description2__c, ContributionCode__c, TotalCost__c, TaxCost__c, CustomerFacingPrice__c, SellForEstimated__c, RelatedQuote__r.GMNC__c, RelatedQuote__r.Name, SystemsOrOptions__r.CostOfTravel__c, 
                                    PercentageOfCost__c, RelatedQuote__r.BaseOptionsEquipmentTotalCost__c, AdHocCost__c, AdHocSegments__c, Type__c, ProductType__c, SystemsOrOptions__r.MechanicalInstallationCost__c, 
                                    SystemsOrOptions__r.ElectricalInstallationCost__c, SystemsOrOptions__r.StartupServicesCost__c, AdhocTotalCost__c
                                    FROM QuoteLineItems__c
                                    WHERE RelatedQuote__c = : qte_sObj.Id
                                    ORDER By Name ASC] ){
                                        qli_List.add( q );
                                    }
        
        for (SystemsOrOptions__c s : [SELECT Id, Name, CostOfTravel__c FROM SystemsOrOptions__c
                                      WHERE Quote__c = : qte_sObj.Id]){
                                          soo_List.add(s);
                                          IF(s.CostofTravel__c != null){
                                              TravelCostTest = (TravelCostTest + s.CostOfTravel__c);
                                              system.debug('TravelCostTest************************' + TravelCostTest);
                                          }
                                          else{
                                              system.debug('Null Value in s.CostOfTravel ' + s.Id);
                                          }
                                      }
        Integer counter = [SELECT count() FROM QuoteLineItems__c WHERE RelatedQuote__c = : qte_sObj.Id];
        
        for( QuoteLineItems__c qli : qli_List ){
            if( qli.ContributionCode__c == '500M1' && qli.TotalCost__c != null){
                TravelCost = 0;
                gmnc = qli.RelatedQuote__r.GMNC__c;
                pem = 0.065 * (qli.TotalCost__c+ (0.047* qli.TotalCost__c));
                pf = 0.047 * qli.TotalCost__c;
                se = 0.132 * qli.TotalCost__c;
                qli.TaxCost__c = pem + pf + se;
                CommissionPercentage = qli.RelatedQuote__r.FinalCommissionAmount__c / qli.RelatedQuote__r.SellForPricewoCommission__c;
                
                //Quotes Gross Margin Net of Commission
                GMNC = qli.RelatedQuote__r.GMNC__c/100;
                CostTotal = qli.TotalCost__c + qli.TaxCost__c;
                TravelCost = (TravelCostTest * (qli.PercentageofCost__c/100));
                SellForWoCommission = ((CostTotal + TravelCost) / (1-GMNC));
                CommissionAmount = ((SellForWoCommission * CommissionPercentage) / (1-GMNC));
                qli.SellForEstimated__c = SellForWoCommission + CommissionAmount;
                EstimatedSellForTotal = qli.SellForEstimated__c + EstimatedSellForTotal;
                
                system.debug('GMNC ' + GMNC);
                system.debug('PEM ' + pem);
                system.debug('SE ' + Se);
                system.debug('PF ' + Pf);
                system.debug('Cost Total ' + CostTotal);
                system.debug('TravelCost '+ TravelCost);
                system.debug('Estimated Sell For w/o Commission ' + SellForWoCommission);
                system.debug('Commission Amount ' + (SellForWoCommission * CommissionPercentage));
                system.debug('Estimated Sell For ' + EstimatedSellForTotal);
                system.debug('QLI Percentage of Cost +' + qli.PercentageofCost__c);
                system.debug('Travel Cost Test ' + TravelCostTest);
            }
        }
        for( QuoteLineItems__c qli : qli_List ){
            if(qli.ContributionCode__c == 'OEM1' && qli.TotalCost__c != null){
                travelCost = 0;
                gmnc = qli.RelatedQuote__r.GMNC__c;
                pf = 0.047 * qli.TotalCost__c/2;
                pem = 0.065 * (qli.TotalCost__c + pf);
                se = 0.132 * qli.TotalCost__c;
                qli.TaxCost__c = (pem + pf + se);
                CommissionPercentage = qli.RelatedQuote__r.FinalCommissionAmount__c / qli.RelatedQuote__r.SellForPricewoCommission__c;
                system.debug('Commission Percentage' + CommissionPercentage);
                
                GMNC = qli.RelatedQuote__r.GMNC__c/100;
                CostTotal = qli.TotalCost__c + qli.TaxCost__c;
                TravelCost = (TravelCostTest * (qli.PercentageofCost__c/100));
                SellForWoCommission = ((CostTotal + TravelCost) / (1-GMNC));
                CommissionAmount = ((SellForWoCommission * CommissionPercentage) / (1-GMNC));
                qli.SellForEstimated__c = SellForWoCommission + CommissionAmount;
                EstimatedSellForTotal = qli.SellForEstimated__c + EstimatedSellForTotal;
                
                system.debug('GMNC ' + GMNC);
                system.debug('PEM ' + pem);
                system.debug('SE ' + Se);
                system.debug('PF ' + Pf);
                system.debug('Cost Total ' + CostTotal);
                system.debug('TravelCost '+ TravelCost);
                system.debug('Estimated Sell For w/o Commission ' + SellForWoCommission);
                system.debug('Commission Amount ' + (SellForWoCommission * CommissionPercentage));
                system.debug('Estimated Sell For ' + EstimatedSellForTotal);
                system.debug('QLI Percentage of Cost +' + qli.PercentageofCost__c);
                system.debug('Travel Cost Test ' + TravelCostTest);
            } 
        }
        for( QuoteLineItems__c qli : qli_List ){
            if(qli.ContributionCode__c == 'CTM1' && qli.TotalCost__c != null){
                TravelCost = 0;
                gmnc = qli.RelatedQuote__r.GMNC__c;
                pf = 0.047 * qli.TotalCost__c/2;
                pem = 0.065 * (pf);
                qli.TaxCost__c = pem + pf;
                CommissionPercentage = qli.RelatedQuote__r.FinalCommissionAmount__c / qli.RelatedQuote__r.SellForPricewoCommission__c;
                
                //Quotes Gross Margin Net of Commission
                GMNC = qli.RelatedQuote__r.GMNC__c/100;
                CostTotal = qli.TotalCost__c + qli.TaxCost__c;
                TravelCost = (TravelCostTest * (qli.PercentageofCost__c/100));
                SellForWoCommission = ((CostTotal + TravelCost) / (1-GMNC));
                CommissionAmount = ((SellForWoCommission * CommissionPercentage) / (1-GMNC));
                qli.SellForEstimated__c = SellForWoCommission + CommissionAmount;
                EstimatedSellForTotal = qli.SellForEstimated__c + EstimatedSellForTotal; 
                
                system.debug('GMNC ' + GMNC);
                system.debug('PEM ' + pem);
                system.debug('SE ' + Se);
                system.debug('PF ' + Pf);
                system.debug('Cost Total ' + CostTotal);
                system.debug('TravelCost '+ TravelCost);
                system.debug('Estimated Sell For w/o Commission ' + SellForWoCommission);
                system.debug('Commission Amount ' + (SellForWoCommission * CommissionPercentage));
                system.debug('Estimated Sell For ' + EstimatedSellForTotal);
                system.debug('QLI Percentage of Cost +' + qli.PercentageofCost__c);
                system.debug('Travel Cost Test ' + TravelCostTest);
            }
        }
    }
}
 
@IsTest
public class ctrl_qliEstimatedSellFor_test {
    static testMethod void Test1(){
        RFQ__c rfqObj                              = new RFQ__c();
        insert rfqObj;
        
        Account a = new Account();
        a.Name = 'Name';
        insert a;
        
        Opportunity opp = new Opportunity();
        opp.StageName = 'Developing Proposal';
        opp.Name = 'test';
        opp.CloseDate = system.today();
        insert opp;
        
        Quotes__c q = new Quotes__c();
        q.RFQ__c = rfqObj.Id;
        q.Name = 'test';
        q.Opportunity__c = opp.Id;
        q.QuoteType__c = 'Budgetary';
        q.MarginPercentage__c = 0.32;
        q.SetSellForPrice__c = 2000000;
        q.CommissionCalculationType__c = 'Shick Standard';
        q.FinalCommissionAmountPB__c = 0;
        insert q;
        
        SystemsOrOptions__c soo = new SystemsOrOptions__c();
        soo.Quote__c = q.Id;
        soo.Locked__c = false;
        soo.Name		= 'Test';
        soo.MechanicalInstallationCost__c = 0;
        soo.ElectricalInstallationCost__c = 0;
        soo.StartUpServicesCost__c = 0;
        insert soo;
        
        QuoteLineItems__c qli = new QuoteLineItems__c();
        qli.RelatedQuote__c = q.Id;
        qli.SystemsOrOptions__c = soo.Id;
        qli.ClonedItem__c = false;
        qli.AdHocCost__c = 4000;
        qli.Qty__c = 100;
        qli.AdHocSegments__c = 'System Custom';
        qli.Type__c = 'Custom';
        qli.ProductType__c = 'Ad-Hoc';
        
        
        insert qli;
               
        system.test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController( q );
        ctrl_qliEstimatedSellFor ctrl = new ctrl_qliEstimatedSellFor( sc );
        
        system.test.stopTest();

    }
    
    static testMethod void Test2(){
        RFQ__c rfqObj                              = new RFQ__c();
        insert rfqObj;
        
        Account a = new Account();
        a.Name = 'Name';
        insert a;
        
        Opportunity opp = new Opportunity();
        opp.StageName = 'Developing Proposal';
        opp.Name = 'test';
        opp.CloseDate = system.today();
        insert opp;
        
        Quotes__c q = new Quotes__c();
        q.RFQ__c = rfqObj.Id;
        q.Name = 'test';
        q.Opportunity__c = opp.Id;
        q.QuoteType__c = 'Budgetary';
        q.MarginPercentage__c = 0.32;
        q.SetSellForPrice__c = 2000000;
        q.CommissionCalculationType__c = 'Shick Standard';
        q.FinalCommissionAmountPB__c = 0;
        insert q;
        
        SystemsOrOptions__c soo = new SystemsOrOptions__c();
        soo.Quote__c = q.Id;
        soo.Locked__c = false;
        soo.Name		= 'Test';
        soo.MechanicalInstallationCost__c = 0;
        soo.ElectricalInstallationCost__c = 0;
        soo.StartUpServicesCost__c = 0;
        insert soo;
        
        QuoteLineItems__c qli = new QuoteLineItems__c();
        qli.RelatedQuote__c = q.Id;
        qli.SystemsOrOptions__c = soo.Id;
        qli.ClonedItem__c = false;
        qli.AdHocCost__c = 4000;
        qli.Qty__c = 100;
        qli.AdHocSegments__c = 'OEM';
        qli.Type__c = 'Custom';
        qli.ProductType__c = 'Ad-Hoc';
        
        insert qli;
               
        system.test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController( q );
        ctrl_qliEstimatedSellFor ctrl = new ctrl_qliEstimatedSellFor( sc );
        
        system.test.stopTest();

    }
    
        static testMethod void Test3(){
        RFQ__c rfqObj                              = new RFQ__c();
        insert rfqObj;
        
        Account a = new Account();
        a.Name = 'Name';
        insert a;
        
        Opportunity opp = new Opportunity();
        opp.StageName = 'Developing Proposal';
        opp.Name = 'test';
        opp.CloseDate = system.today();
        insert opp;
        
        Quotes__c q = new Quotes__c();
        q.RFQ__c = rfqObj.Id;
        q.Name = 'test';
        q.Opportunity__c = opp.Id;
        q.QuoteType__c = 'Budgetary';
        q.MarginPercentage__c = 0.32;
        q.SetSellForPrice__c = 2000000;
        q.CommissionCalculationType__c = 'Shick Standard';
        q.FinalCommissionAmountPB__c = 0;
        insert q;
        
        SystemsOrOptions__c soo = new SystemsOrOptions__c();
        soo.Quote__c = q.Id;
        soo.Locked__c = false;
        soo.Name		= 'Test';
        soo.MechanicalInstallationCost__c = 0;
        soo.ElectricalInstallationCost__c = 0;
        soo.StartUpServicesCost__c = 0;
        insert soo;
        
        QuoteLineItems__c qli = new QuoteLineItems__c();
        qli.RelatedQuote__c = q.Id;
        qli.SystemsOrOptions__c = soo.Id;
        qli.ClonedItem__c = false;
        qli.AdHocCost__c = 4000;
        qli.Qty__c = 100;
        qli.AdHocSegments__c = 'Controls';
        qli.Type__c = 'Controls';
        qli.ProductType__c = 'Ad-Hoc';
        
        insert qli;
               
        system.test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController( q );
        ctrl_qliEstimatedSellFor ctrl = new ctrl_qliEstimatedSellFor( sc );
        
        system.test.stopTest();

    }
}
Best Answer chosen by NathanW.
Amit Chaudhary 8Amit Chaudhary 8
Look like issue is coming because your qli.PercentageofCost__c is null in test clas. Try to pass value in your test class for qli.PercentageofCost__c field or add null check in your Apex Class like below 

Example code for line 64

if (qli.PercentageofCost__c != null){
TravelCost = (TravelCostTest * (qli.PercentageofCost__c/100));
}

Or update your test class like below
QuoteLineItems__c qli = new QuoteLineItems__c();
147        qli.RelatedQuote__c = q.Id;
148        qli.SystemsOrOptions__c = soo.Id;
149        qli.ClonedItem__c = false;
150        qli.AdHocCost__c = 4000;
151        qli.Qty__c = 100;
152        qli.AdHocSegments__c = 'Controls';
153        qli.Type__c = 'Controls';
154        qli.ProductType__c = 'Ad-Hoc';
155         qli.PercentageofCost__c = 10;
156        insert qli;