I'm getting System.NullPointerException: Attempt to de-reference a null object when I run the test class, but the batch class will run without any issue.

Here is my batch class:

global class CHS_Account_Survey_Invitation_BatchClass implements Database.Batchable<sObject> {

    global static List<String> terrNameSet = new List<String>();
    global static List<SurveyInvitation> survInvList = [select id,OCE__Account__c, OCE__Territory__c, OCE__Type__c from SurveyInvitation WHERE OCE__Type__c = 'Account' AND OCE__Territory__c != NULL];
    global static void setTerrName(){
        for(SurveyInvitation surInvRec : survInvList){
    global Database.QueryLocator start(Database.BatchableContext BC){
        string query;
            query = 'select id,OCE__Account__c, OCE__Territory__c, CHS_No_of_Territory_Survey_Invitation__c from OCE__AccountTerritoryFields__c WHERE OCE__Account__r.IsPersonAccount =TRUE AND OCE__Territory__c != Null LIMIT 1 ';            
            system.debug('>>>Query :'+query);
            query = 'select id,OCE__Account__c, OCE__Territory__c, CHS_No_of_Territory_Survey_Invitation__c from OCE__AccountTerritoryFields__c WHERE OCE__Account__r.IsPersonAccount =TRUE AND OCE__Territory__c IN :terrNameSet ';            
       return  Database.getQueryLocator(query);

    global void execute(Database.BatchableContext BC, List<OCE__AccountTerritoryFields__c> scope){
        List<OCE__AccountTerritoryFields__c> updateAtfList = new List<OCE__AccountTerritoryFields__c>();

        Map<String, List<SurveyInvitation>> terrSurInvMap = new Map<String, List<SurveyInvitation>>();

        for(SurveyInvitation surInvRec : survInvList){
            system.debug('>>>surInvRec: '+surInvRec);
            if(surInvRec.OCE__Account__c == Null){
                    system.debug('>>>terrSurInvMap_1: '+surInvRec +terrSurInvMap.get(surInvRec.OCE__Territory__c));
                    terrSurInvMap.put(surInvRec.OCE__Territory__c, new List<SurveyInvitation> {surInvRec});
                    system.debug('>>>terrSurInvMap_2: '+surInvRec +terrSurInvMap.get(surInvRec.OCE__Territory__c));
        for(OCE__AccountTerritoryFields__c atfRec : scope){
            system.debug('>>>atfRec: '+atfRec );
            system.debug('>>>surInvCount: '+terrSurInvMap.get(atfRec.OCE__Territory__c));
            Integer surInvCount = terrSurInvMap.get(atfRec.OCE__Territory__c).size();
            if(surInvCount != NULL && surInvCount != 0 && atfRec.CHS_No_of_Territory_Survey_Invitation__c != surInvCount){
                atfRec.CHS_No_of_Territory_Survey_Invitation__c = surInvCount;
           update  updateAtfList;

        catch(Exception ex){
            system.debug('>>>Error : '+ex.getMessage());

    global void finish(Database.BatchableContext BC){


Here is my test class:

@isTest(SeeAllData = true)
private class CHS_Account_Survey_Invitation_TestClass {
    private static testMethod void test(){

        List<Survey> surList = [select id,Name from survey];
        Survey surRec = surList[0];
        system.debug('>>>surRec: '+surRec);
        Territory terrRec = [select id, Name from Territory LIMIT 1];
        system.debug('>>>terrRec: '+terrRec);       

        SurveyInvitation surInvRec = new SurveyInvitation ();
        surInvRec.OCE__Type__c = 'Account';
        surInvRec.OCE__Territory__c = terrRec.Name;
        surInvRec.SurveyId = surRec.Id;
        surInvRec.Name = 'Test Survey Invitation';

        insert surInvRec;
        system.debug('>>>surInvRec: '+surInvRec);

        Account accRec = new Account();
        accRec.FirstName = 'test';
        accRec.LastName = 'account';
        accRec.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('MP (Medical Professional)').getRecordTypeId();
        accRec.OCE__Territories__c = terrRec.Name;

        insert accRec;
        system.debug('>>>accRec: '+accRec);

        OCE__AccountTerritoryFields__c atfRec = new OCE__AccountTerritoryFields__c();
        atfRec.OCE__Account__c = accRec.Id;
        atfRec.OCE__Territory__c = terrRec.Name;

        insert atfRec;
        system.debug('>>>atfRec: '+atfRec);


            CHS_Account_Survey_Invitation_BatchClass batchClass = new CHS_Account_Survey_Invitation_BatchClass();
            Database.executeBatch(batchClass, 1);
            system.debug('>>>enter execution');


The batch will count the number of surveys shared per territory and update it in (survey invitation) No of Territory Survey Invitation field of ATF object which is acustom object. The batch job is running and updating the count properly when run from anonymous window, but when run the batch job - Attempt to de-reference a null object error is coming at System.Test.stopTest() line.

Here is the debug log error message 09:17:02.928 (11928345607)|CUMULATIVE_LIMIT_USAGE_END

09:17:02.723 (11929705925)|HEAP_ALLOCATE|[19]|Bytes:41

09:17:02.723 (11929740055)|METHOD_EXIT|[43]||System.Test.stopTest()

09:17:02.723 (11929800757)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

09:17:02.723 (11929811571)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

  • 1
    Welcome. Please add more info to the question. As it stands, you've just copied and pasted your code and told us to figure out what's the issue. Some pertinent info would be: what line does the error reference in your code and what do your debug statements show when you run the test? Oct 29, 2019 at 11:22
  • Please add the new information into your original question by clicking edit. Oct 29, 2019 at 11:38

1 Answer 1


You have a flaw in your code:

Integer surInvCount = terrSurInvMap.get(atfRec.OCE__Territory__c).size();

When the result of get() is null, then size() will throw an exception.

Note that the return value from size() will never be null:

if(surInvCount != NULL && ...

Instead, check for the null value from the map first:

SurveyInvitation[] inventory = terrSurInvMap.get(atfRec.OCE__Territory__c);
if(inventory != null) {
    Integer surInvCount = inventory.size();
    if(surInvCount > 0 && surInvCount != atfRec.CHS_No_of_Territory_Survey_Invitation__c) {
  • updated with above code, but still getting the same error
    – user75627
    Oct 29, 2019 at 12:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .