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


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

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';

            insert t;
            Program__c pr=new Program__c();//insert Program
            pr.Name='Test Program ';
            insert pr;
            Program_Session__c psR=new Program_Session__c();
        Database.SaveResult []str = Database.insert(ps,false);
        countContactRecordsBatch obj=new countContactRecordsBatch();
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
Not getting now any error.
But stuck in code coverage.