+ Start a Discussion

System.NullPointerException: Attempt to de-refernce a null object V2

This has reared its ugly head once again. 
Ran this in Sandbox and was fine until it ran in Production. 

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

Class.VolumeMarketBatch.execute: line 39, column 1
global with sharing class VolumeMarketBatch implements Database.batchable<sObject> {  // updated implements 1-13-2019 nfs
    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){ // added 1-13-2019 nfs
        // 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) {

Right.  So if Line [39] is referencing a and pc, then it's got to be one of those that's null. 

Since each of them is set from a SOQL-generated Map only a line or two earlier, and there's no reason to think the Maps themselves would have null values in them, the problem must be that the keys used to retrieve these values do not exist in those Maps.

I don't know what you've done to troubleshoot so far, so apologies if you're already past this step, but I would start by adding some system.debug() statements to see which is the null value (a or pc) as well as the values of zipAssign.Remodeler_Account__c and zipAssign.Postal_Code__c right before you get the error.  You might also want to add debug() statements for the Maps themselves, to at least make sure they have some results in them, even if you can't see all the results.

That should at least give you some clue as to what's different between your Sandbox and your Production environments.  (Note that if you are using database.executebatch() to test and generate log files, it may take a few for the log from the execute() method to show up.  It should still show up in the same place, though.)