Skip to main content
1 of 2

Test Class for the below code

i have written a test class for the following code, but it throwing a error like "Attempt to dereference a null object". can anyone please help me to overcome this.

public class leadclass {

public static void methodconcatenate(list<lead> newleadlist){
   // list<lead> updateleadlist;
    string fullname;
    map<string, candidate__c> candidatemap = new map<string, candidate__c>();
    
    for(lead l:newleadlist){
        if(l.firstname != null && l.lastname != null){
            fullname= l.firstname+ ' ' +l.lastname;
        }
    }
    for(candidate__c c : [select id, name from candidate__c where name=:fullname]){
        candidatemap.put(fullname,c);
    }
    
    for(lead l :newleadlist){
        if(candidatemap != null && !candidatemap.isempty() && candidatemap.containskey(l.firstname+ ' ' +l.lastname))
        l.candidate__c = candidatemap.get(l.firstname+ ' ' +l.lastname).id;
       // updateleadlist.add(l);
    }
   // update updateleadlist;
}

}

Test Class: @isTest public class testclassforlead { Static Testmethod void myunitTest(){ string fullname; list newleadlist = new list();

    Lead ll = new lead();
    ll.firstname = 'Ishwarya';
    ll.lastname = 'Sekar';
    insert ll;
    
    Candidate__c cc = new Candidate__c();
    cc.Name = 'Ishwarya Sekar';
    insert cc;
    
    map<string, candidate__c> candidatemap = new map<string, candidate__c>();
    
    test.startTest(); 
    if(ll.FirstName != null && ll.LastName != null){
        fullname= ll.firstname+ ' ' +ll.lastname;
        cc.Name = fullname;
        
        for(candidate__c c : [select id, name from candidate__c where name=:fullname]){
        candidatemap.put(fullname,c);
    }
    
    for(lead l :newleadlist){
        if(candidatemap != null && !candidatemap.isempty() && candidatemap.containskey(l.firstname+ ' ' +l.lastname))
        l.candidate__c = candidatemap.get(l.firstname+ ' ' +l.lastname).id;
    }
        update ll;
    }
    
    test.stopTest();
}

}