+ Start a Discussion
Derek Patrick DayaDerek Patrick Daya 

Test Class Error: System.NullPointerException: Attempt to de-reference a null object

I am receiving this error on my test class which I do not understand why it occurred as it's working before I added a new function to it.

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DerekUpdateResumeSource: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.DerekUpdateResumeSource: line 41, column 1: []

Stack Trace Class.DerekUpdateSource.updateUpdateSource: line 139, column 1

==========

Original Trigger without Error:

@IsTest
public class DerekUpdateSource {

    static testmethod void updateUpdateSource(){

        AVTRRT__Config_Settings__c mycs = AVTRRT__Config_Settings__c.getValues('Default');
    if(mycs == null)
    {
        mycs = new AVTRRT__Config_Settings__c(Name= 'Default');
        mycs.Trigger_Disable_UpdateAccountName__c = true;
        insert mycs;
    }
        Contact c = new contact();
            c.LastName = 'Test';
            c.RecordtypeID = '012A0000000v0La';
            c.AVTRRT__Source__c = null;
        insert c;
       
        AVTRRT__ETCObject__c a = new AVTRRT__ETCObject__c();
            a.AVTRRT__Name__c = 'jobsDB';
            a.AVTRRT__Candidate__c = c.id;
        insert a;       
        AVTRRT__ETCObject__c b = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'jobsDB' and AVTRRT__Candidate__c = :c.Id limit 1];
        update b;

        Contact c1 = new contact();
            c1.LastName = 'Test1';
            c1.RecordtypeID = '012A0000000v0La';
            c1.AVTRRT__Source__c = null;
        insert c1;
       
        AVTRRT__ETCObject__c e = new AVTRRT__ETCObject__c();
            e.AVTRRT__Name__c = 'Clyde Marine Recruitment';
            e.AVTRRT__Candidate__c = c1.id;
        insert e;
        AVTRRT__ETCObject__c f = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Clyde Marine Recruitment' and AVTRRT__Candidate__c = :c1.Id limit 1];
        update f;
       
        Contact c2 = new contact();
            c2.LastName = 'Test2';
            c2.RecordtypeID = '012A0000000v0La';
            c2.AVTRRT__Source__c = null;
        insert c2;
       
        AVTRRT__ETCObject__c h = new AVTRRT__ETCObject__c();
            h.AVTRRT__Name__c = 'Corporate Event';
            h.AVTRRT__Candidate__c = c2.id;
        insert h;
        AVTRRT__ETCObject__c f1 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Corporate Event' and AVTRRT__Candidate__c = :c2.Id limit 1];
        update f1;
       
        Contact c3 = new contact();
            c3.LastName = 'Test3';
            c3.RecordtypeID = '012A0000000v0La';
            c3.AVTRRT__Source__c = null;
        insert c3;
       
        AVTRRT__ETCObject__c k = new AVTRRT__ETCObject__c();
            k.AVTRRT__Name__c = 'e Financial Careers';
            k.AVTRRT__Candidate__c = c3.id;
        insert k;
        AVTRRT__ETCObject__c f3 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'e Financial Careers' and AVTRRT__Candidate__c = :c3.Id limit 1];
        update f3;
       
        Contact c4 = new contact();
            c4.LastName = 'Test4';
            c4.RecordtypeID = '012A0000000v0La';
            c4.AVTRRT__Source__c = null;
        insert c4;
       
        AVTRRT__ETCObject__c n = new AVTRRT__ETCObject__c();
            n.AVTRRT__Name__c = 'Head Hunt';
            n.AVTRRT__Candidate__c = c4.id;
        insert n;
        AVTRRT__ETCObject__c f4 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Head Hunt' and AVTRRT__Candidate__c = :c4.Id limit 1];
        update f4;
       
        Contact c5 = new contact();
            c5.LastName = 'Test5';
            c5.RecordtypeID = '012A0000000v0La';
            c5.AVTRRT__Source__c = null;
        insert c5;
       
        AVTRRT__ETCObject__c q = new AVTRRT__ETCObject__c();
            q.AVTRRT__Name__c = 'Indeed';
            q.AVTRRT__Candidate__c = c5.id;
        insert q;
        AVTRRT__ETCObject__c f5 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Indeed' and AVTRRT__Candidate__c = :c5.Id limit 1];
        update f5;     
       
        Contact c6 = new contact();
            c6.LastName = 'Test6';
            c6.RecordtypeID = '012A0000000v0La';
            c6.AVTRRT__Source__c = null;
        insert c6;
       
        AVTRRT__ETCObject__c t = new AVTRRT__ETCObject__c();
            t.AVTRRT__Name__c = 'Jobstreet';
            t.AVTRRT__Candidate__c = c6.id;
        insert t;
        AVTRRT__ETCObject__c f6 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Jobstreet' and AVTRRT__Candidate__c = :c6.Id limit 1];
        update f6;
       
        Contact c7 = new contact();
            c7.LastName = 'Test7';
            c7.RecordtypeID = '012A0000000v0La';
            c7.AVTRRT__Source__c = null;
        insert c7;
       
        AVTRRT__ETCObject__c w = new AVTRRT__ETCObject__c();
            w.AVTRRT__Name__c = 'LinkedIn - Resourcer';
            w.AVTRRT__Candidate__c = c7.id;
        insert w;
        AVTRRT__ETCObject__c f7 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'LinkedIn - Resourcer' and AVTRRT__Candidate__c = :c7.Id limit 1];
        update f7;
       
        Contact c8 = new contact();
            c8.LastName = 'Test8';
            c8.RecordtypeID = '012A0000000v0La';
            c8.AVTRRT__Source__c = null;
        insert c8;
       
        AVTRRT__ETCObject__c a1 = new AVTRRT__ETCObject__c();
            a1.AVTRRT__Name__c = 'Linkedin Advert';
            a1.AVTRRT__Candidate__c = c8.id;
        insert a1;
        AVTRRT__ETCObject__c f8 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Linkedin Advert' and AVTRRT__Candidate__c = :c8.Id limit 1];
        update f8;     
               
}
}

When added the code below to the above code, it gives me the error:

Contact c22 = new contact();
            c22.LastName = 'Test22';
            c22.RecordtypeID = '012A0000000v0La';
            c22.AVTRRT__Source__c = null;
        insert c22;
       
        AVTRRT__ETCObject__c a22 = new AVTRRT__ETCObject__c();
            a22.AVTRRT__Name__c = 'Red Website';
            a22.AVTRRT__Candidate__c = c22.id;
        insert a22;       
        AVTRRT__ETCObject__c b22 = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'Red Website' and AVTRRT__Candidate__c = :c22.Id limit 1];
        update b22;

=======

I really do not understand why the error occured as the code I added is the same as what other codes are which is working. Its a bit frustrating as I am already at 68% code coverage. I just need to add that to it to make it 75%.

Mani RenusMani Renus
Can you please post your Trigger then it will be easy to solve your case
Derek Patrick DayaDerek Patrick Daya
Hi mani,

This is the trigger.

trigger DerekUpdateResumeSource on AVTRRT__ETCObject__c (after insert, after update) {
   
    Map<Id, AVTRRT__ETCObject__c> ETCmap = new Map<Id,AVTRRT__ETCObject__c>();
    for(AVTRRT__ETCObject__c a:trigger.new)
    {

            ETCmap.put(a.AVTRRT__Candidate__c, a);

           
    }
    List<Contact> candidate = [Select Id, AVTRRT__Source__c from Contact WHERE ID IN:ETCmap.keyset() AND RecordtypeID = '012A0000000v0La'];
    List<Contact> ContactsToUpdate = new List<Contact>();
    system.debug(candidate.size());
    for(Contact c:candidate)
    {
       If(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('jobsDB') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('JobsDB') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('jobsdb') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Jobsdb')))
       {
               c.AVTRRT__Source__c = 'JobsDB';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Clyde Marine Recruitment') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('clyde'))){
               c.AVTRRT__Source__c = 'Clyde Marine Recruitment';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Corporate Event') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('coorporate event'))){
               c.AVTRRT__Source__c = 'Corporate Event';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('e Financial Careers') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('e financial career'))){
               c.AVTRRT__Source__c = 'e Financial Careers';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Head Hunt') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('head hunt'))){
               c.AVTRRT__Source__c = 'Head Hunt';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Indeed') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('indeed'))){
               c.AVTRRT__Source__c = 'Indeed';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Jobstreet') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('jobstreet') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Job Street') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('job street'))){
               c.AVTRRT__Source__c = 'Jobstreet';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('LinkedIn - Resourcer') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linked In - Recruiter') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linkedin - Recruiter') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('linkedin - recruiter'))){
               c.AVTRRT__Source__c = 'LinkedIn - Resourcer';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linkedin Advert') || ETCmap.get(c.Id).AVTRRT__From__c.contains('job-apps@linkedin.com'))){
               c.AVTRRT__Source__c = 'Linkedin Advert';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('LinkedIn Recruiter - Consultant') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linkedin Recruiter - Consultant') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('linkedin recruiter - consultant') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('linked in recruiter - consultant'))){
               c.AVTRRT__Source__c = 'LinkedIn Recruiter - Consultant';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Oil&Gas JobSearch') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Oil & Gas JobSearch') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Oil&Gas Job Search') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('oil&gas jobsearch') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('oil & gas jobsearch') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('CV Alert'))){
               c.AVTRRT__Source__c = 'Oil&Gas JobSearch';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('OilCareers') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Oil Careers') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('oilcareers') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('oil careers'))){
               c.AVTRRT__Source__c = 'OilCareers';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Reed') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('reed'))){
               c.AVTRRT__Source__c = 'Reed';
               ContactsToUpdate.add(c);
            } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Red Website') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('red website'))){
               c.AVTRRT__Source__c = 'Red Website';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Referrals') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('referrals') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('referral') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Referral'))){
               c.AVTRRT__Source__c = 'Referrals';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('S1 Jobs') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('s1jobs') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('s1 Jobs') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('s1 jobs'))){
               c.AVTRRT__Source__c = 'S1 Jobs';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Straits Times') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('straits times'))){
               c.AVTRRT__Source__c = 'Straits Times';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Trade Winds') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('trade winds') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('trade wind') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('TradeWinds') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('TradeWind') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('tradewind'))){
               c.AVTRRT__Source__c = 'TradeWinds';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('UK Job Centre') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('uk job centre') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('UK job centre'))){
               c.AVTRRT__Source__c = 'UK Job Centre';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == null && (ETCmap.get(c.Id).AVTRRT__Name__c.contains('Jobs Bank') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('jobs bank') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('Job Bank') || ETCmap.get(c.Id).AVTRRT__Name__c.contains('job bank'))){
               c.AVTRRT__Source__c = 'Jobs Bank';
               ContactsToUpdate.add(c);
           } else {
                c.AVTRRT__Source__c = 'Other';
                ContactsToUpdate.add(c);
           }
    }
    update ContactsToUpdate;
    }