0

I have a batch apex where I insert some records. I check on the fields for != null but still get the error:

Attempt to de-reference a null object

Here is my batch part:

global void execute(Database.BatchableContext bc, List<Relatie_Contact__c> scope){
        // process each batch of records  

        if(scope.size()!=Null){
            for (Relatie_Contact__c rc : Scope){
                if(Scope.size() > 0) {

                    if(rc.Contactpersoon__r.Birthdate.year() !=null){
                    Integer St=System.Today().year()- rc.Contactpersoon__r.Birthdate.year();

                    Id relatietype;
                    if(rc.relatietype__c == 'Zomer Challenge' && rc.relatietype__c!=null){ 
                        relatietype = relatietype4[0].id;
                    }

                        if(St < 6 && rc.relatietype__c != 'Zomer Challenge' && rc.relatietype__c!=null){ 
                            relatietype = relatietype3[0].id;
                        }
                        if(St > 5 && St <= 17 && rc.relatietype__c != 'Zomer Challenge' && rc.relatietype__c!=null){  
                            relatietype = relatietype2[0].id;
                        }
                        if(St > 17 && rc.relatietype__c != 'Zomer Challenge' && rc.relatietype__c!=null){  
                            relatietype = relatietype1[0].id;
                        }

                    //If Referentie__c > 50 characters --> error in servicebus     
                    if(rc.Contactpersoon__r.Bondsnummer__c != null && rc.Contactpersoon__r.Name !=null){
                    Intlenght = (String.valueOf(rc.Contactpersoon__r.Bondsnummer__c +' / ' + rc.Contactpersoon__r.Name)).length();
                    if(Intlenght >49 ){subjectInput = (rc.Contactpersoon__r.Bondsnummer__c +' / ' + rc.Contactpersoon__r.Name).subString(0,49) ;}
                    if(Intlenght <49 ){subjectInput = (rc.Contactpersoon__r.Bondsnummer__c +' / ' + rc.Contactpersoon__r.Name);}     
                    }
                    Factuurregel__c fa =new Factuurregel__c(Organisatie__c=rc.Organisatie__c , Debiteur__c =rc.Organisatie__r.Debiteurennummer__c,
                                                            Aantal__c = 1, AFAS_ID__c=relatietype, Factuurdatum__c= date.today(), 
                                                            Contactpersoon__c = rc.Contactpersoon__c, Referentie__c = subjectInput
                                                           );
                    fact.add(fa); 
                } 
            }}insert fact; fact.clear();}
    }    

The following line seems to throw the error:

Integer St=System.Today().year()- rc.Contactpersoon__r.Birthdate.year();
  • 4
    The error probably comes from the if statement itself. If the Birthdate field is set to null, you can't call the year() method on it. You should check if the Birthdate field is not null. – Martin Lezer May 2 '18 at 9:33
  • 2
    'seems' to throw the error? Does it throw the error, or are you just guessing? Why are you checking if(scope.size()!=Null) at the start? The size of scope will never be null. – Nick Cook May 2 '18 at 9:38
  • Martin Lezer is probably right. Put a System.debug before your if parameter to figure out what is null. You can run your your batch with small amount of records. – oozkan May 2 '18 at 9:39
  • @MartinLezer could you edit this as the answer as this was the culprit. Thanks! – Thomas May 2 '18 at 10:17
  • A system.debug on a null object would also throw a NPE. Check the Birthdate against null before doing the system.debug! – crmprogdev May 2 '18 at 12:56
0

The error probably comes from the if statement itself. If the Birthdate field is set to null, you can't call the year() method on it. You should check if the Birthdate field is not null. – Martin Lezer May 2 at 9:33

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.