You need to instantiate your list outside of the IF() statement. When its only getting instantiated within an IF(), there are times when your IF() statement is false, and thus the list is never instantiated.
Then you use that list (that was never instantiated) in an IF statement, and its throwing an error.
When the IF() statement below returns false (your set of IDs is empty), then the list never gets instantiated
if (!leadToContact.isEmpty()) {
List<Handoff__c> lstHandoff = new List<Handoff__c>();
//More Code
//More Code
}
But you are still trying to use that list (which was never instantiated) later in this IF() statement. This is what I would suspect is causing the error
if (!lstHandoff.isEmpty()){ // 1stHandoff doesn't EXIST in this scenario
//code
}
Something like this should fix it (just moved the instantiation of the list outside of the IF() statement.
List<Handoff__c> lstHandoff = new List<Handoff__c>();
if (!leadToContact.isEmpty()) {
for(Handoff__c handoffs:[select Id,Contact__c from Handoff__c where Lead__c in : leadToContact.keySet()]){
if(leadToContact.containsKey(handoffs.id)){
handoffs.Contact__c = leadToContact.get(handoffs.id);
lstHandoff.add(handoffs);
}
}
}