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
Ashu sharma 38Ashu sharma 38 

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

Hi,

I am creating a test class for updating a field ,but there is one issue am getting an error System.NullPointerException: Attempt to de-reference a null object

@isTest
public class countContactRecordsBatchTest {
    
    static testMethod void contactRecords(){
        list<Program_Session__c> ps=new list<Program_Session__c>();
        
                    Account a=new account();
                    a.Name='Test Account';
                    insert a;
            hed__Term__c  t=new hed__Term__c();//insert Program term
            t.Name='Test Term';

            t.hed__Account__r.id=a.Id;
            t.Census_Date__c=date.today();
            insert t;
            
            Program__c pr=new Program__c();//insert Program
            pr.Name='Test Program ';
            insert pr;
            
            Program_Session__c psR=new Program_Session__c();
            psR.Program__r.name=pr.Name;
            psR.Term__r.name=t.Name;
            
            ps.add(psR);
            
            
        }
        
        test.startTest();
        Database.SaveResult []str = Database.insert(ps,false);
        countContactRecordsBatch obj=new countContactRecordsBatch();
        database.executeBatch(obj);
        
    }
}
Danish HodaDanish Hoda
Hi Ashu,

I think your object - Program_Session__c is having lookup with Program and Term objects.
For this you need to match the Ids and the names like below:

Program_Session__c psR = new Program_Session__c();
psR.Program__c = pr.Id;
psR.Term__c = t.Id;
Ashu sharma 38Ashu sharma 38
Yes...
 cou.hed__Account__c=acc[0].Id;
Not getting now any error.
But stuck in code coverage.

Thanks.