+ Start a Discussion
HNT_NeoHNT_Neo 

Help Understanding Apex Class

Hi Everyone, 

I need help in making sense of this Apex class that was created years ago in our org. 

Today we ran into an Apex script unhandled exception 

Failed to process batch for class 'VolumeMarketBatch' 

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

Class.VolumeMarketBatch.execute: line 36, column 1

line 36 below is: 
a.Volume_Market__c = pc.Volume_Market__c;​​​​​​​

Any help is greatly appreciated. 

Thank you

 
global with sharing class VolumeMarketBatch implements Database.batchable<sObject> {

	
	global Database.QueryLocator start(Database.BatchableContext info) {

		// Go through and get the list of volume market that has changed
		String returnQuery = 'select Id, Remodeler_Account__c, Postal_Code__c ' +
							 '	from Zip_Assignment__c ' +  
							 '	where Postal_Code__r.Volume_Market_Update__c = true';

        return Database.getQueryLocator(returnQuery); 
   
	}
	
	global void execute(Database.BatchableContext info, List<sObject> sobjectList) {
		// Gets the account and postal codes that have changed
		List<Id> accountIdList = new List<Id>();
		List<Id> pcIdList = new List<Id>();
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
			accountIdList.add(zipAssign.Remodeler_Account__c);
			pcIdList.add(zipAssign.Postal_Code__c);
		}
		
		// Goes through the accounts and postal codes that have changed and sets the Volume Market field on the account
		Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c from Account where Id = : accountIdList]);
		Map<Id, Postal_Code__c> pcList = new Map<Id, Postal_Code__c>([select Id, Volume_Market_Update__c, Volume_Market__c from Postal_Code__c where Id = : pcIdList]);
		
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
			Account a = accountList.get(zipAssign.Remodeler_Account__c);
			Postal_Code__c pc = pcList.get(zipAssign.Postal_Code__c);
			//System.Debug(pc.Volume_Market__c + a.Volume_Market__c);
			if (pc != null) { 
				if (pc.Volume_Market__c != null) {
					a.Volume_Market__c = pc.Volume_Market__c;
				}
				pc.Volume_Market_Update__c = false;
			}
		}
		
		// updates both account and postal codes
		upsert accountList.values();
		upsert pcList.values();
	}
	
	global void finish(Database.BatchableContext info) {
		
	
	}

}

 
Best Answer chosen by HNT_Neo
Alain CabonAlain Cabon
Hi,

if (zipAssign.Remodeler_Account__c != null && zipAssign.Postal_Code__c != null)
 
global with sharing class VolumeMarketBatch implements Database.batchable<sObject> {

	
	global Database.QueryLocator start(Database.BatchableContext info) {

		// Go through and get the list of volume market that has changed
		String returnQuery = 'select Id, Remodeler_Account__c, Postal_Code__c ' +
							 '	from Zip_Assignment__c ' +  
							 '	where Postal_Code__r.Volume_Market_Update__c = true';

        return Database.getQueryLocator(returnQuery); 
   
	}
	
	global void execute(Database.BatchableContext info, List<sObject> sobjectList) {
		// Gets the account and postal codes that have changed
		List<Id> accountIdList = new List<Id>();
		List<Id> pcIdList = new List<Id>();
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c != null && zipAssign.Postal_Code__c != null) {
			    accountIdList.add(zipAssign.Remodeler_Account__c);
			    pcIdList.add(zipAssign.Postal_Code__c);
            }
		}
		
		// Goes through the accounts and postal codes that have changed and sets the Volume Market field on the account
		Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c from Account where Id = : accountIdList]);
		Map<Id, Postal_Code__c> pcList = new Map<Id, Postal_Code__c>([select Id, Volume_Market_Update__c, Volume_Market__c from Postal_Code__c where Id = : pcIdList]);
		
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c == null || zipAssign.Postal_Code__c == null) {
                  // not correct: move to the next directly (not possible here but the test is redone to be sure)
                  continue;
            }
			Account a = accountList.get(zipAssign.Remodeler_Account__c);
			Postal_Code__c pc = pcList.get(zipAssign.Postal_Code__c);
			//System.Debug(pc.Volume_Market__c + a.Volume_Market__c);
			if (pc != null) { 
				if (pc.Volume_Market__c != null) {
					a.Volume_Market__c = pc.Volume_Market__c;
				}
				pc.Volume_Market_Update__c = false;
			}
		}
		
		// updates both account and postal codes
		upsert accountList.values();
		upsert pcList.values();
	}
	
	global void finish(Database.BatchableContext info) {
		
	}
}

 

All Answers

Alain CabonAlain Cabon
Hi,

You should test if  zipAssign.Remodeler_Account__c != null (perhaps)

for (sObject obj : sobjectList) {
          Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
          if (zipAssign.Remodeler_Account__c != null) {
                   accountIdList
.add(zipAssign.Remodeler_Account__c);
                   pcIdList.add(zipAssign.Postal_Code__c);
          }          
}

 Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c fromAccount where Id = : accountIdList]);
 Account a = accountList.get(zipAssign.Remodeler_Account__c);
 
HNT_NeoHNT_Neo
Received these two errors with the updated code below: 


User-added image
 
global with sharing class VolumeMarketBatch implements Database.batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext info) {
        
        // Go through and get the list of volume market that has changed
        String returnQuery = 'select Id, Remodeler_Account__c, Postal_Code__c ' +
            '	from Zip_Assignment__c ' +  
            '	where Postal_Code__r.Volume_Market_Update__c = true';
        
        return Database.getQueryLocator(returnQuery); 
    }
    
    global void execute(Database.BatchableContext info, List<sObject> sobjectList) {
        // Gets the account and postal codes that have changed
        List<Id> accountIdList = new List<Id>();
        List<Id> pcIdList = new List<Id>();
        for (sObject obj : sobjectList) {
            Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            accountIdList.add(zipAssign.Remodeler_Account__c);
            pcIdList.add(zipAssign.Postal_Code__c);
        }
        
        // Goes through the accounts and postal codes that have changed and sets the Volume Market field on the account
        Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c 
                                                             from fromAccount where Id = : accountIdList]);
        Map<Id, Postal_Code__c> pcList = new Map<Id, Postal_Code__c>([select Id, Volume_Market_Update__c, Volume_Market__c from Postal_Code__c where Id = : pcIdList]);
        
        for (sObject obj : sobjectList) {
            Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c != null) {
                accountIdList.add(zipAssign.Remodeler_Account__c);
                pcIdList.add(zipAssign.Postal_Code__c);
            }          
        }
        Account a = accountList.get(zipAssign.Remodeler_Account__c);
        Postal_Code__c pc = pcList.get(zipAssign.Postal_Code__c);
        //System.Debug(pc.Volume_Market__c + a.Volume_Market__c);
        if (pc != anull) { 
            if (pc.Volume_Market__c != null) {
                a.Volume_Market__c = pc.Volume_Market__c;
            }
            pc.Volume_Market_Update__c = false;
        }
    }
    // updates both account and postal codes
    upsert accountList.values();
    upsert pcList.values();
}
global void finish(Database.BatchableContext info) {
    }
}

 
Amit RayAmit Ray
Please paste the error code/message  here as font size is very very small
HNT_NeoHNT_Neo
//ERROR 1
Expecting '}' but was: 'upsert'


//ERROR 2
Class VolumeMarketBatch must implement the method: void Database.Batchable<SObject>.finish(Database.BatchableContext)



//LATEST APEX CODE in SANDBOX

global with sharing class VolumeMarketBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext info) {
        
        // Go through and get the list of volume market that has changed
        String returnQuery = 'select Id, Remodeler_Account__c, Postal_Code__c ' +
            '	from Zip_Assignment__c ' +  
            '	where Postal_Code__r.Volume_Market_Update__c = true';
        
        return Database.getQueryLocator(returnQuery); 
    }
    
    global void execute(Database.BatchableContext info, List<sObject> sobjectList) {
        // Gets the account and postal codes that have changed
        List<Id> accountIdList = new List<Id>();
        List<Id> pcIdList = new List<Id>();
        for (sObject obj : sobjectList) {
            Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            accountIdList.add(zipAssign.Remodeler_Account__c);
            pcIdList.add(zipAssign.Postal_Code__c);
        }
        
        // Goes through the accounts and postal codes that have changed and sets the Volume Market field on the account
        //Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c 
        //	from fromAccount where Id = : accountIdList]);
        Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c 
                                                             from Account where Id = : accountIdList]); // modified from
        
        Map<Id, Postal_Code__c> pcList = new Map<Id, Postal_Code__c>([select Id, Volume_Market_Update__c, Volume_Market__c from Postal_Code__c where Id = : pcIdList]);
        
        for (sObject obj : sobjectList) {
            Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c != null) {
                accountIdList.add(zipAssign.Remodeler_Account__c);
                pcIdList.add(zipAssign.Postal_Code__c);
            }
        }
        Account a = accountList.get(zipAssign.Remodeler_Account__c);
        Postal_Code__c pc = pcList.get(zipAssign.Postal_Code__c);
        //System.Debug(pc.Volume_Market__c + a.Volume_Market__c);
        if (pc != anull) { 
            if (pc.Volume_Market__c != null) {
                a.Volume_Market__c = pc.Volume_Market__c;
            }
            pc.Volume_Market_Update__c = false;
        }
    }
    
    // updates both account and postal codes
    upsert accountList.values();
    upsert pcList.values();
}

global void finish(Database.BatchableContext info) {
	}
}

 
Alain CabonAlain Cabon
Hi,

if (zipAssign.Remodeler_Account__c != null && zipAssign.Postal_Code__c != null)
 
global with sharing class VolumeMarketBatch implements Database.batchable<sObject> {

	
	global Database.QueryLocator start(Database.BatchableContext info) {

		// Go through and get the list of volume market that has changed
		String returnQuery = 'select Id, Remodeler_Account__c, Postal_Code__c ' +
							 '	from Zip_Assignment__c ' +  
							 '	where Postal_Code__r.Volume_Market_Update__c = true';

        return Database.getQueryLocator(returnQuery); 
   
	}
	
	global void execute(Database.BatchableContext info, List<sObject> sobjectList) {
		// Gets the account and postal codes that have changed
		List<Id> accountIdList = new List<Id>();
		List<Id> pcIdList = new List<Id>();
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c != null && zipAssign.Postal_Code__c != null) {
			    accountIdList.add(zipAssign.Remodeler_Account__c);
			    pcIdList.add(zipAssign.Postal_Code__c);
            }
		}
		
		// Goes through the accounts and postal codes that have changed and sets the Volume Market field on the account
		Map<Id, Account> accountList = new Map<Id, Account>([select Id, Volume_Market__c from Account where Id = : accountIdList]);
		Map<Id, Postal_Code__c> pcList = new Map<Id, Postal_Code__c>([select Id, Volume_Market_Update__c, Volume_Market__c from Postal_Code__c where Id = : pcIdList]);
		
		for (sObject obj : sobjectList) {
			Zip_Assignment__c zipAssign = (Zip_Assignment__c) obj;
            if (zipAssign.Remodeler_Account__c == null || zipAssign.Postal_Code__c == null) {
                  // not correct: move to the next directly (not possible here but the test is redone to be sure)
                  continue;
            }
			Account a = accountList.get(zipAssign.Remodeler_Account__c);
			Postal_Code__c pc = pcList.get(zipAssign.Postal_Code__c);
			//System.Debug(pc.Volume_Market__c + a.Volume_Market__c);
			if (pc != null) { 
				if (pc.Volume_Market__c != null) {
					a.Volume_Market__c = pc.Volume_Market__c;
				}
				pc.Volume_Market_Update__c = false;
			}
		}
		
		// updates both account and postal codes
		upsert accountList.values();
		upsert pcList.values();
	}
	
	global void finish(Database.BatchableContext info) {
		
	}
}

 
This was selected as the best answer
HNT_NeoHNT_Neo
Hi Alain, 

I am grateful for your help! 

It is now good to go!

Was wondering if you use something else than Developer Console to help with coding with Apex? 
 
HNT_NeoHNT_Neo
HI Alain, 

Check this out. 

Same Issue, but different location of the code. 

If you have time, can you please check this out: 

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000g7IV#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9062I000000g7LyQAI