1

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<lead> newleadlist = new list<lead>();

        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();
    }
}
1

The problem is in this line:

candidatemap.put(fullname,c);

You should be putting the record's name here:

candidatemap.put(c.Name,c);

Secondly, this code could be simplified:

if(candidatemap != null && !candidatemap.isempty() && candidatemap.containskey(l.firstname+ ' ' +l.lastname))

To just:

if(l.firstName!=null && candidatemap.containskey(l.firstname+ ' ' +l.lastname))

Your unit test doesn't appear to be directly calling your main class, so you'll need to edit it to call the class by doing the appropriate DML operations or calling the method directly.

  • Thanks a lot. It worked fine for me!! – iswaryasekar Feb 7 at 8:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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