function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
yujohwan99yujohwan99 

"Attempting to dereference a null object" error with working code?

When I make a new contact to test this code, it works perfectly fine. However, when trying to mass update over 20000 or so records, I receieve an "attempt to dereference a null object" error on the for each loop. I'm not sure why this is the case. Is this because I hit a governor limit? 

List<String> joinDate = new List<String>();

Schema.DescribeFieldResult fieldResult = Contact.JoinDate__c.getDescribe();
List<Schema.PicklistEntry> pEntries = fieldResult.getPicklistValues();

for (Schema.PicklistEntry pEntry : pEntries)
{
    String pe = pEntry.getValue();
    if (pe.startsWith('1') || pe.startsWith('200') || 
        (pe.startsWith('201') && !pe.equals('2019'))){
        joinDate.add(pe);
    }
}

List<Contact> massupdate = new List<Contact>();

List<Contact> cons = [SELECT Id, Name, npe01_WorkEmail__c 
                     FROM Contact 
                     WHERE Join_Date__c IN :joinDate];
system.debug('Number of records in list above' + cons.size());

For(Contact c: cons){
    if(c.npe01_WorkEmail__c.endsWithIgnoreCase('it.work.com')){
        System.debug('>>>>>>>>>' + c.npe01_WorkEmail__c);
        c.npe01_WorkEmail__c = null;
        massupdate.add(c);
    }
}

if(!massupdate.isEmpty()){
    update massupdate;
}
Raj VakatiRaj Vakati
Try this
 
Set<String> joinDate = new Set<String>();

Schema.DescribeFieldResult fieldResult = Contact.JoinDate__c.getDescribe();
List<Schema.PicklistEntry> pEntries = fieldResult.getPicklistValues();

for (Schema.PicklistEntry pEntry : pEntries)
{
    String pe = pEntry.getValue();
    if (pe.startsWith('1') || pe.startsWith('200') || 
        (pe.startsWith('201') && !pe.equals('2019'))){
        joinDate.add(pe);
    }
}

List<Contact> massupdate = new List<Contact>();

List<Contact> cons = [SELECT Id, Name, npe01_WorkEmail__c 
                     FROM Contact 
                     WHERE Join_Date__c IN :joinDate];
system.debug('Number of records in list above' + cons.size());

For(Contact c: cons){
    if(c.npe01_WorkEmail__c.endsWithIgnoreCase('it.work.com') && c.npe01_WorkEmail__c!=null){
        System.debug('>>>>>>>>>' + c.npe01_WorkEmail__c);
        c.npe01_WorkEmail__c = null;
        massupdate.add(c);
    }
}

if(!massupdate.isEmpty()){
    update massupdate;
}