I'm getting these Errors NullPointerException: Attempt to de-reference a null object. and the line are: Class.LeadConvertUtil.convertLead: line 30, column 1 Class.Test_LeadConvertUtil.leadConvertUtilTest: line 58, column 1 how to fix it?
the Class code is:
public class LeadConvertUtil {
public static void convertLead(Lead leadRcd){
String leadRecordTypeName = leadRcd.Lead_Record_Type_Name__c;
System.debug('Lead Record Type :'+leadRecordTypeName);
String convertLeadId = leadRcd.Id;
List<RecordType> accountRecordTypes = [select Id, Name
from RecordType
where SobjectType = 'Account'
and Name=:leadRecordTypeName];
List<RecordType> contactRecordTypes = [select Id, Name
from RecordType
where SobjectType = 'Contact' and Name=:leadRecordTypeName];
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(convertLeadId);
lc.setDoNotCreateOpportunity(true);
LeadStatus convertStatus;
try{
convertStatus = [SELECT Id, MasterLabel
FROM LeadStatus
WHERE IsConverted=true and MasterLabel='Converted' and Id != null];
}catch(System.QueryException e){
System.debug('caught ya!');
}
lc.setConvertedStatus(convertStatus.MasterLabel);
try{
System.debug('Converting Lead with id '+convertLeadId);
Database.LeadConvertResult lcr = Database.convertLead(lc);
String accntId = lcr.getAccountId();
String cntId = lcr.getContactId();
System.debug('getAccountId: '+ accntId);
System.debug('getContactId: '+ cntId);
System.debug('getLeadId: '+ lcr.getLeadId());
System.debug('isSuccess: '+ lcr.isSuccess());
//Account Updates
Account convertedAccount;
try{
convertedAccount = [Select id from Account where id=:accntId];
}catch(Exception e){
convertedAccount =null;
}
if(accountRecordTypes!=null && accountRecordTypes.size()>0){
convertedAccount.recordTypeId = accountRecordTypes.get(0).Id;
}
convertedAccount.Converted_Lead_ID__c = convertLeadId;
update convertedAccount;
//Contact Updates
if(contactRecordTypes!=null && contactRecordTypes.size()>0){
Contact convertedContact = [Select id from Contact where id=:cntId];
convertedContact.recordTypeId = contactRecordTypes.get(0).Id;
update convertedContact;
}
} catch(DmlException e) {
System.debug('Exception: '+ e);
}
}
}
the Test class code is:
/**
* This class contains unit tests for validating the behavior of LeadConvertUtil
*
*/
@isTest
public class Test_LeadConvertUtil{
static testMethod void leadConvertTrigrTest() {
//test.startTest();
//Lead VPS Record Type
// BEGIN: S-444340, T-567488 - include Lead record type = 'IAA VPS Sales'
List<RecordType> leadRecordTypes = [select Id, Name
from RecordType
where SobjectType = 'Lead' and Name='IAA VPS Sales'];
//Insert Test Lead
Lead ldTst = new Lead();
ldTst.FirstName = 'Lead1';
ldTst.LastName = 'Lead';
ldTst.email = '[email protected]';
ldTst.Company = 'TestCorp';
ldTst.Contract_Type__c = 'CFF';
if(leadRecordTypes!=null && leadRecordTypes.size()>0){
ldTst.recordTypeId = leadRecordTypes.get(0).Id;
}
ldTst.Lead_Stage__c = 'Account Activated';
ldTst.Status ='Converted';
insert ldTst;
}
static testMethod void leadConvertUtilTest() {
//Lead VPS Record Type
// BEGIN: S-444340, T-567488 - include Lead record type = 'IAA VPS Sales'
List<RecordType> leadRecordTypes = [select Id, Name
from RecordType
where SobjectType = 'Lead' and Name='IAA VPS Sales'];
//Insert Test Lead
Lead ldTst = new Lead();
ldTst.FirstName = 'Lead1';
ldTst.LastName = 'Lead';
ldTst.email = '[email protected]';
ldTst.Company = 'TestCorp';
ldTst.Contract_Type__c = 'CFF';
//ldTst.RecordType.Name = 'IAA VPS Sales';
if(leadRecordTypes!=null && leadRecordTypes.size()>0){
ldTst.recordTypeId = leadRecordTypes.get(0).Id;
}
ldTst.Lead_Stage__c = 'Account Activated';
ldTst.Status ='Converted';
insert ldTst;
LeadConvertUtil.convertLead(ldTst);
//test.stopTest();
}
}