3

I am writing this code and I have all the kinks worked out except 1. Despite all my efforts I can't figure out why This code wont validate in production.

This is my error code:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, onDependent: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.onNewDependent.newRow: line 57, column 1 Trigger.onDependent: line 2, column 1: [] Stack Trace: Class.testOnDependent.verifyChange: line 16, column 1

This is my code:

public class onNewDependent {

    public static void newRow(dependant__c[] depdts)
    {
        Premium_Rate__c rate = new Premium_Rate__c();
        for (dependant__c dep :depdts)
        {
            if (dep !=null){
         if(dep.Age__c.equals('0')||dep.Age__c.equals('1')||dep.Age__c.equals('2')||dep.Age__c.equals('3')||dep.Age__c.equals('4')||dep.Age__c.equals('5')||dep.Age__c.equals('6')||dep.Age__c.equals('7')||dep.Age__c.equals('8')||dep.Age__c.equals('9')||dep.Age__c.equals('10')||dep.Age__c.equals('11')||dep.Age__c.equals('12')||dep.Age__c.equals('13')||dep.Age__c.equals('14')||dep.Age__c.equals('15')||dep.Age__c.equals('16')||dep.Age__c.equals('17')||dep.Age__c.equals('18')||dep.Age__c.equals('19')||dep.Age__c.equals('20')){dep.age_rate__c = 0.635;}
         else if(dep.Age__c.equals('21')){dep.age_rate__c = 1.0;}   
         else if(dep.Age__c.equals('22')){dep.age_rate__c = 1.0;}  
         else if(dep.Age__c.equals('23')){dep.age_rate__c = 1.0;}  
         else if(dep.Age__c.equals('24')){dep.age_rate__c = 1.0;}  
         else if(dep.Age__c.equals('25')){dep.age_rate__c = 1.004;}  
         else if(dep.Age__c.equals('26')){dep.age_rate__c = 1.024;}  
         else if(dep.Age__c.equals('27')){dep.age_rate__c = 1.048;}  
         else if(dep.Age__c.equals('28')){dep.age_rate__c = 1.087;}  
         else if(dep.Age__c.equals('29')){dep.age_rate__c = 1.119;}  
         else if(dep.Age__c.equals('30')){dep.age_rate__c = 1.135;}  
         else if(dep.Age__c.equals('31')){dep.age_rate__c = 1.159;}  
         else if(dep.Age__c.equals('32')){dep.age_rate__c = 1.183;}   
         else if(dep.Age__c.equals('33')){dep.age_rate__c = 1.198;}  
         else if(dep.Age__c.equals('34')){dep.age_rate__c = 1.214;}  
         else if(dep.Age__c.equals('35')){dep.age_rate__c = 1.222;}  
         else if(dep.Age__c.equals('36')){dep.age_rate__c = 1.23;}  
         else if(dep.Age__c.equals('37')){dep.age_rate__c = 1.238;}  
         else if(dep.Age__c.equals('38')){dep.age_rate__c = 1.246;}  
         else if(dep.Age__c.equals('39')){dep.age_rate__c = 1.262;}  
         else if(dep.Age__c.equals('40')){dep.age_rate__c = 1.278;}  
         else if(dep.Age__c.equals('41')){dep.age_rate__c = 1.302;}  
         else if(dep.Age__c.equals('42')){dep.age_rate__c = 1.325;} 
         else if(dep.Age__c.equals('43')){dep.age_rate__c = 1.357;}  
         else if(dep.Age__c.equals('44')){dep.age_rate__c = 1.397;}  
         else if(dep.Age__c.equals('45')){dep.age_rate__c = 1.444;}  
         else if(dep.Age__c.equals('46')){dep.age_rate__c = 1.5;}  
         else if(dep.Age__c.equals('47')){dep.age_rate__c = 1.563;}
         else if(dep.Age__c.equals('48')){dep.age_rate__c = 1.635;} 
         else if(dep.Age__c.equals('49')){dep.age_rate__c = 1.706;}  
         else if(dep.Age__c.equals('50')){dep.age_rate__c = 1.786;}  
         else if(dep.Age__c.equals('51')){dep.age_rate__c = 1.865;}  
         else if(dep.Age__c.equals('52')){dep.age_rate__c = 1.952;}  
         else if(dep.Age__c.equals('53')){dep.age_rate__c = 2.04;}
         else if(dep.Age__c.equals('54')){dep.age_rate__c = 2.135;}
         else if(dep.Age__c.equals('55')){dep.age_rate__c = 2.23;}
         else if(dep.Age__c.equals('56')){dep.age_rate__c = 2.333;}
         else if(dep.Age__c.equals('57')){dep.age_rate__c = 2.437;}
         else if(dep.Age__c.equals('58')){dep.age_rate__c = 2.548;}
         else if(dep.Age__c.equals('59')){dep.age_rate__c = 2.603;}
         else if(dep.Age__c.equals('60')){dep.age_rate__c = 2.714;}
         else if(dep.Age__c.equals('61')){dep.age_rate__c = 2.81;}
         else if(dep.Age__c.equals('62')){dep.age_rate__c = 2.873;}
         else if(dep.Age__c.equals('63')){dep.age_rate__c = 2.952;}
         else if(dep.Age__c.equals('64')){dep.age_rate__c = 3.0;}
         else if(dep.Age__c.equals('65')||dep.Age__c.equals('66')||dep.Age__c.equals('67')||dep.Age__c.equals('68')||dep.Age__c.equals('69')||dep.Age__c.equals('70')||dep.Age__c.equals('71')||dep.Age__c.equals('72')||dep.Age__c.equals('73')||dep.Age__c.equals('74')||dep.Age__c.equals('75')||dep.Age__c.equals('76')||dep.Age__c.equals('77')||dep.Age__c.equals('78')||dep.Age__c.equals('79')||dep.Age__c.equals('80')||dep.Age__c.equals('81')||dep.Age__c.equals('82')||dep.Age__c.equals('83')||dep.Age__c.equals('84')||dep.Age__c.equals('85')||dep.Age__c.equals('86')||dep.Age__c.equals('87')||dep.Age__c.equals('88')||dep.Age__c.equals('89')||dep.Age__c.equals('90')||dep.Age__c.equals('91')||dep.Age__c.equals('92')||dep.Age__c.equals('93')||dep.Age__c.equals('94')||dep.Age__c.equals('95')||dep.Age__c.equals('96')||dep.Age__c.equals('97')||dep.Age__c.equals('98')||dep.Age__c.equals('99')||dep.Age__c.equals('100')||dep.Age__c.equals('101')||dep.Age__c.equals('102')||dep.Age__c.equals('103')||dep.Age__c.equals('104')||dep.Age__c.equals('105'))
         {dep.age_rate__c = 3.0;}else{dep.age_rate__c = 0.635;}

         if(dep.rating_region__c.contains('Region 1')){dep.rating_region_number__c=1.000;}
         if(dep.rating_region__c.contains('Region 2'))//checks rating regions
         {dep.rating_region_number__c=1.009;}      
         if(dep.rating_region__c.contains('Region 3'))//checks rating regions
         {dep.rating_region_number__c=0.984;}  
         if(dep.rating_region__c.contains('Region 4'))//checks rating regions
         {dep.rating_region_number__c=1.033;}

       decimal  baseRate = [SELECT Premium_Rate__c.Base_Rate__c FROM Premium_Rate__c WHERE Premium_Rate__c.id = :dep.Premium_Rate__c].Base_Rate__c;

       decimal ayy = [SELECT Premium_Rate__c.Plan_rate__c FROM Premium_Rate__c where Premium_Rate__c.id = :dep.Premium_Rate__c].Plan_rate__c;
        string aplus = String.valueOf(ayy);

        decimal byy = [SELECT Premium_Rate__c.Individual_Plan_Rate__c FROM Premium_Rate__c where Premium_Rate__c.id = :dep.Premium_Rate__c].Individual_Plan_Rate__c;

        if(string.isEmpty(aplus))
        {dep.Plan_Rate__c = byy;}
        else{dep.Plan_Rate__c = ayy;}


               if((dep.sequence_number__c<=3 && (dep.Age__c.equals('0')||dep.Age__c.equals('1')||dep.Age__c.equals('2')||dep.Age__c.equals('3')||dep.Age__c.equals('4')||dep.Age__c.equals('5')||dep.Age__c.equals('6')||dep.Age__c.equals('7')||dep.Age__c.equals('8')||dep.Age__c.equals('9')||dep.Age__c.equals('10')||dep.Age__c.equals('11')||dep.Age__c.equals('12')||dep.Age__c.equals('13')||dep.Age__c.equals('14')||dep.Age__c.equals('15')||dep.Age__c.equals('16')||dep.Age__c.equals('17')||dep.Age__c.equals('18')||dep.Age__c.equals('19')||dep.Age__c.equals('20')||dep.Age__c.equals('21'))&& dep.relationship__c.equals('Dependent'))||(dep.relationship__c.equals('Dependent')&& (dep.Age__c.equals('22')||dep.Age__c.equals('23')||dep.Age__c.equals('24')||dep.Age__c.equals('25')||dep.Age__c.equals('26')||dep.Age__c.equals('27')||dep.Age__c.equals('28')||dep.Age__c.equals('29')||dep.Age__c.equals('30')||dep.Age__c.equals('31')||dep.Age__c.equals('32')||dep.Age__c.equals('33')||dep.Age__c.equals('34')||dep.Age__c.equals('35')||dep.Age__c.equals('36')||dep.Age__c.equals('37')||dep.Age__c.equals('38')||dep.Age__c.equals('39')||dep.Age__c.equals('40')||dep.Age__c.equals('41')||dep.Age__c.equals('42')||dep.Age__c.equals('43')||dep.Age__c.equals('44')||dep.Age__c.equals('45')||dep.Age__c.equals('46')||dep.Age__c.equals('47')||dep.Age__c.equals('48')||dep.Age__c.equals('49')||dep.Age__c.equals('50')||dep.Age__c.equals('51')||dep.Age__c.equals('52')||dep.Age__c.equals('53')||dep.Age__c.equals('54')||dep.Age__c.equals('55')||dep.Age__c.equals('56')||dep.Age__c.equals('57')||dep.Age__c.equals('58')||dep.Age__c.equals('59')||dep.Age__c.equals('60')||dep.Age__c.equals('61')||dep.Age__c.equals('62')||dep.Age__c.equals('63')||dep.Age__c.equals('64')||dep.Age__c.equals('65')||dep.Age__c.equals('66')||dep.Age__c.equals('67')||dep.Age__c.equals('68')||dep.Age__c.equals('69')||dep.Age__c.equals('70')||dep.Age__c.equals('71')||dep.Age__c.equals('72')||dep.Age__c.equals('73')||dep.Age__c.equals('74')||dep.Age__c.equals('75')||dep.Age__c.equals('76')||dep.Age__c.equals('77')||dep.Age__c.equals('78')||dep.Age__c.equals('79')||dep.Age__c.equals('80')||dep.Age__c.equals('81')||dep.Age__c.equals('82')||dep.Age__c.equals('83')||dep.Age__c.equals('84')||dep.Age__c.equals('85')||dep.Age__c.equals('86')||dep.Age__c.equals('87')||dep.Age__c.equals('88')||dep.Age__c.equals('89')||dep.Age__c.equals('90')||dep.Age__c.equals('91')||dep.Age__c.equals('92')||dep.Age__c.equals('93')||dep.Age__c.equals('94')||dep.Age__c.equals('95')||dep.Age__c.equals('96')||dep.Age__c.equals('97')||dep.Age__c.equals('98')||dep.Age__c.equals('99')||dep.Age__c.equals('100')))||dep.relationship__c.equals('Spouse'))
       dep.final_rate__c = dep.Plan_rate__c*baserate*dep.age_rate__c*dep.rating_region_number__c;            
        }
        }
    }
}

this is my test class:

@isTest(seealldata=true)
public class testOnDependent {

    @isTest
    public static void verifyChange()
    {
        Premium_Rate__c rate = new Premium_Rate__c(Name = 'john',
                                                   Age_Del__c = '99',
                                                   Base_rate__c = 1.000,
                                                   final_rate__c=1.000,
                                                   Plan__c = 'cs',
                                                   plan_rate__c=1.000,
                                                   rating_region__c='region 1',
                                                   rating_regions__c=1.000,
                                                   Relationship__c='Single',
                                                   Relationship_Rate__c=1.000,
                                                   Smoker__c=true,
                                                   smoker_rate__c=1.000,
                                                   small_group_plan_year__c = '2015',Individual_Rate_Year__c = '2016');
        insert rate;
        dependant__c dep = new dependant__c(Premium_Rate__c = 'john',
                                            Name='sdkjflsdkfj',
                                            Age__c ='65',
                                            relationship__c = 'Dependent'
                                            /*Rating_Region__c='Region 1'
                                            age_rate__c = 1.000,
                                            Plan_Rate__c = 1.000
                                            sequence_number__c = 1*/);
        if (dep!=null)
        {insert dep;        
        Test.startTest();
        dep.Age__c = '65';
        dep.relationship__c = 'Spouse';
        dep.Rating_Region__c='Region 1';
        dep.Plan_Rate__c = 1.000;
        update dep;       
        dep.Age__c = '21';
        dep.Rating_Region__c='Region 2';
        dep.relationship__c = 'Dependent';
        update dep; 
        dep.age_rate__c = 1.000;
        dep.Age__c = '22';
        update dep; 
        dep.Age__c = '23';
        update dep; 
        dep.Age__c = '24';
        update dep; 
        dep.Age__c = '25';
        update dep; 
        dep.Age__c = '26';
        update dep; 
        dep.Age__c = '27';
        update dep; 
        dep.Age__c = '28';
        update dep; 
        dep.Age__c = '29';
        update dep; 
        dep.Age__c = '30';
        update dep; 
        dep.Age__c = '31';
        update dep; 
        dep.Age__c = '32';
        update dep; 
        dep.Age__c = '33';
        update dep; 
        dep.Age__c = '34';
        update dep; 
        dep.Age__c = '35';
        update dep; 
        dep.Age__c = '36';
        update dep;
        dep.Age__c = '37';
        update dep; 
        dep.Age__c = '38';
        update dep; 
        dep.Age__c = '39';
        update dep; 
        dep.Age__c = '40';
        update dep; 
        dep.Age__c = '41';
        update dep; 
        dep.Age__c = '42';
        update dep; 
        dep.Age__c = '43';
        update dep; 
        dep.Age__c = '44';
        update dep; 
        dep.Age__c = '45';
        update dep; 
        dep.Age__c = '46';
        update dep; 
        dep.Age__c = '47';
        update dep; 
        dep.Age__c = '48';
        update dep; 
        dep.Age__c = '49';
        update dep; 
        dep.Age__c = '50';
        update dep; 
        dep.Age__c = '51';
        dep.Age__c = '52';
        update dep; 
        dep.Age__c = '53';
        update dep; 
        dep.Age__c = '54';
        update dep; 
        dep.Age__c = '55';
        update dep; 
        dep.Age__c = '56';
        update dep; 
        dep.Age__c = '57';
        update dep; 
        dep.Age__c = '58';
        update dep; 
        dep.Age__c = '59';
        update dep; 
        dep.Age__c = '60';
        update dep; 
        test.stopTest();
        }
    }
}

This is my trigger:

trigger onDependent on dependant__c (before insert, before update) {
    onNewDependent.newRow(trigger.new);
}
  • Hi Andy, you have changed this to a completely different question. Please roll back your change and ask that separately. When you radically change your question after subsequent fixes, it reduces the value of your question. – Adrian Larson Mar 8 '16 at 18:52
4

You need to wrap your checks against rating_region__c in a null check.

if (String.isNotBlank(dep.rating_region__c))
{
     if(dep.rating_region__c.contains('Region 1')){dep.rating_region_number__c=1.000;}
     if(dep.rating_region__c.contains('Region 2'))//checks rating regions
     {dep.rating_region_number__c=1.009;}      
     if(dep.rating_region__c.contains('Region 3'))//checks rating regions
     {dep.rating_region_number__c=0.984;}  
     if(dep.rating_region__c.contains('Region 4'))//checks rating regions
     {dep.rating_region_number__c=1.033;}
}

While you're in there refactoring, you might want to consider using a Map instead of a massive or chain. You could even populate this map via List Custom Setting and make it configurable!

static final Integer fallbackRate = .635;
static final Map<String, Decimal> ageToRate = new Map<String, Decimal>
{
    '0' => .635, '1' => .635, // ...
    '21' => 1, '22' => 1, '23' => 1, '24' => 1, '25' => 1.004,
    //...
    '65' => 3, '66' => 3 // ...
}

Then you can change that huge code block to:

if (ageToRate.contains(dep.Age__c))
    dep.Age_Rate__c = ageToRate.get(dep.Age__c)
else
    dep.Age_Rate__c = fallbackRate;

Note that since your fallback is .635, you don't really need to map an Age Rate for 1-20.

  • Ill try that and let you know. do I need to wrap all checks if there is a potential for the item to be null? – Andy Scott Mar 8 '16 at 15:45
  • 1
    Also, the first block of code would have the same error if the age field were null. Using .equals should never be used if the value may be null. – sfdcfox Mar 8 '16 at 15:48
  • 4
    @sfdcfox Getting to that. Recommending a map structure, it just takes more typing haha. – Adrian Larson Mar 8 '16 at 15:49
  • I'll leave you to it, then. – sfdcfox Mar 8 '16 at 15:50
  • 3
    As well as using a map, I'd also suggest storing the mapping in a custom setting/custom metadata - looks like those values could change over time? – BritishBoyinDC Mar 8 '16 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.