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
MrHammyMrHammy 

System.NullPointerException: Attempt to de-reference a null object

workign with someone elses code, we ran into a bulk DML issue, I set up a list to hold the updates untill after the for loop but now I am geting

System.NullPointerException: Attempt to de-reference a null object

I feel it is something to do with the loop but am not sure what, any help would be appreciated ,

here is the class:



public class CustomerSalesOrgManager {

public static void UpdateCustomerSalesOrg (   MDMCustomer.UpdateRequest customer)    {
   

    // first, get the ERP_Data__c records which are linked to this MDS Code
   
    try {               
    List<ERP_Data__c> erpData = [select MDS_Salesperson_Code__c,
         Name,  
         MDS_Salesperson_Name__c,
         MDS_Territory_Code__c,
         MDS_Territory_Name__c,
         MDS_Region_Code__c,
         MDS_Region_Name__c,
         MDS_District_Code__c,
         MDS_District_Name__c,
         RecordTypeId,
         BU_SOURCE_SYSTEM__c  
        from ERP_Data__c
        where MDS_Code__c =: customer.code for update];
       
            // build a list to update at the very end to prevent limit issues dgh 2/13/2014
        List<ERP_Data__c> updatedList = new List<ERP_Data__c>();
              
        for(ERP_Data__c record:erpData) {
            System.debug(record.Name + 'found');
                   
            // for every record linked this way, update the sales org fields based upon record type
            if ((record.BU_SOURCE_SYSTEM__c.contains('LC'))) {
                // update as LCI Customer
                    if (customer.LCITerritory != null)
                    {              
                     record.MDS_Territory_Code__c = customer.LCITerritory.Code;
                     record.MDS_Territory_Name__c = customer.LCITerritory.Name;                    
                    }
                    if (customer.LCIRegion != null)
                    {                  
                     record.MDS_Region_Code__c = customer.LCIRegion.Code;                    
                     record.MDS_Region_Name__c = customer.LCIRegion.Name;                    
                    }                    
            }        
            else if ((record.RecordTypeId=='0128000000020mT') || (record.RecordTypeId=='0128000000020mO') || (record.RecordTypeId=='0128000000020mV'))
            {
                 // update as EWS customer
                if (customer.EWSSalesperson != null) {                
                     record.MDS_Salesperson_Code__c = customer.EWSSalesperson.Code;
                     record.MDS_Salesperson_Name__c = customer.EWSSalesperson.Name;           
                }
                if (customer.EWSTerritory != null) {                
                     record.MDS_Territory_Code__c = customer.EWSTerritory.Code;
                     record.MDS_Territory_Name__c = customer.EWSTerritory.Name;
                }
                if (customer.EWSRegion != null) {
                     record.MDS_Region_Code__c = customer.EWSRegion.Code;
                     record.MDS_Region_Name__c = customer.EWSRegion.Name;                                 
                }                
            }
            else if (record.RecordTypeId=='0128000000020mJ') {
                // update as Wattstopper customer
                if (customer.WSSalesRep != null) {              
                    record.MDS_Salesperson_Code__c = customer.WSSalesRep.Code;
                    record.MDS_Salesperson_Name__c = customer.WSSalesRep.Name;           
                }
                if (customer.WSDistrict != null) {               
                    record.MDS_District_Code__c = customer.WSDistrict.Code;
                    record.MDS_District_Name__c = customer.WSDistrict.Name;
                }
                if (customer.WSRegion != null) {               
                    record.MDS_Region_Code__c = customer.WSRegion.Code;
                    record.MDS_Region_Name__c = customer.WSRegion.Name;                              
                }               
            }
            else if ((record.RecordTypeId=='0128000000026Bx') || (record.RecordTypeId=='0128000000026C7') || (record.RecordTypeId=='012C0000000G9MU')) {
                //update as Home Systems customer
               
                
            }
            else if (record.RecordTypeId=='0128000000020mU') {
                // update as Ortronics customer
                if (customer.ORRepAgency != null) {               
                    record.MDS_Salesperson_Code__c = customer.ORRepAgency.Code;
                    record.MDS_Salesperson_Name__c = customer.ORRepAgency.Name;
                }
                if (customer.ORRegion != null) {               
                    record.MDS_Region_Code__c = customer.ORRegion.Code;
                    record.MDS_Region_Name__c = customer.ORRegion.Name;                     
                }
            }    
                    
            updatedList.add(record);
        }
         update updatedList;  
    }
   
   
    catch (System.QueryException qex){
        // no erp records tied to this MDS Code
        } 
}

}
Sonam_SFDCSonam_SFDC
You must be recieving the line number in the error message which states the Nullpointerexception"System.NullPointerException: Attempt to de-reference a null object"

This error comes in when you have an atribute/variable/object in your controller you are using (on that line number mentioned in the error message)which is null. before using it in any method - check to see if its NULL..