You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
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;
cou.hed__Account__c=acc[0].Id;
Not getting now any error.
But stuck in code coverage.
Thanks.