You need to sign in to do that
Don't have an account?
Ghanesan
test class - System.NullPointerException: Attempt to de-reference a null object
Getting this error for the below apex test class:
Apex Handler:
public class InsertIncomeRecordHandler {
public static void insertRecord(List<Salary_Detail__c> salList,Map<id,Salary_Detail__c> oldMap){
Set<Id> empId = new Set<Id>();
List<D_Transaction__c> dList = new List<D_Transaction__c>();
ID income = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Income').getRecordTypeId();
ID trans = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('Transaction').getRecordTypeId();
ID expense = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Expense').getRecordTypeId();
for(Salary_Detail__c sd : salList){
if(sd.Advance__c != oldMap.get(sd.Id).Advance__c && sd.Isdetected__c !=oldMap.get(sd.Id).Isdetected__c){
if(sd.Advance__c >0 && sd.RecordTypeId == trans && sd.Organization__c == 'Deshkal'){
empId.add(sd.Employee_Information__c);
}
}
system.debug(' empIdt' + empId);
}
for(Employee_Information__c emp : [select id,Name From Employee_Information__c where id IN :empId]){
for(D_Transaction__c dt : [SELECT RecordTypeId,D_Amount__c,D_Employee__c,D_Advance_Status__c,D_Advance_Repay__c,D_Name__c FROM D_Transaction__c where D_Employee__c =:emp.Id AND RecordTypeId =:expense AND D_Advance_Status__c = 'Open' ]){
D_Transaction__c d = new D_Transaction__c();
d.D_Employee__c = emp.Id;
d.RecordTypeId = income;
d.D_Transaction__c =dt.Id;
Integer result = integer.valueOf(dt.D_Amount__c) / integer.valueOf(dt.D_Advance_Repay__c);
d.D_Amount__c = result;
system.debug('d.D_Amount__c');
dList.add(d);
}
}
if(!dList.isEmpty()){
insert dList;
}
}
}
Test Class:
@isTest
private class InsertIncomeRecordHandlerTest {
@isTest
static void testInsertRecord() {
// Create test data
Employee_Information__c emp = new Employee_Information__c();
emp.Salutation__c = 'Mr.';
emp.Name = 'Test';
emp.Employee_Last_Name__c = 'HR Admin';
emp.Date_of_Join__c = system.today();
emp.Gender__c = 'Male';
emp.Personal_Email__c = 'abc@gmail.com';
emp.Marital_Status__c = 'Single';
emp.Employment_Status__c = 'Active';
emp.DN_Designation__c='CEO';
emp.Date_of_Birth__c=Date.newInstance(2000, 12, 9);
emp.Gender__c='Male';
emp.Manager_Supervisor__c = 'Deepu H D';
emp.Work_Location__c = 'NOC';
emp.Bank_Name__c='HDFC';
emp.Account_Number__c='12345678901234';
emp.Mobile_Number__c='9999999999';
emp.IFSC_CODE__c='HDFC0000003';
emp.Confirmed_CTC__c=800000;
emp.Basic_Percentage__c='40';
emp.Tax_Regime__c='Old Regime';
emp.PFs__c='Percentage';
emp.Marital_Status__c='Single';
emp.Aadhaar_Number__c='265385644663';
emp.Address__c='Test';
emp.City__c='Chennai';
emp.Organization__c='Deshkal';
emp.Postal_Code__c='600001';
emp.DN_Leave_Process__c='Yes';
insert emp;
Salary_Detail__c salary = new Salary_Detail__c(
Employee_Information__c = emp.Id,
RecordTypeId = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('Transaction').getRecordTypeId(),
Advance__c = 60,
Isdetected__c = false,
Organization__c = 'Deshkal'
);
insert salary;
D_Transaction__c dt = new D_Transaction__c ();
dt.D_Employee__c = emp.Id;
dt.D_Amount__c = 100;
dt.D_Advance_Repay__c =' 12';
dt.RecordTypeId = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('expense').getRecordTypeId();
dt.D_Advance_Status__c = 'Open';
Test.startTest();
insert dt;
Test.stopTest();
}
}
Apex Handler:
public class InsertIncomeRecordHandler {
public static void insertRecord(List<Salary_Detail__c> salList,Map<id,Salary_Detail__c> oldMap){
Set<Id> empId = new Set<Id>();
List<D_Transaction__c> dList = new List<D_Transaction__c>();
ID income = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Income').getRecordTypeId();
ID trans = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('Transaction').getRecordTypeId();
ID expense = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Expense').getRecordTypeId();
for(Salary_Detail__c sd : salList){
if(sd.Advance__c != oldMap.get(sd.Id).Advance__c && sd.Isdetected__c !=oldMap.get(sd.Id).Isdetected__c){
if(sd.Advance__c >0 && sd.RecordTypeId == trans && sd.Organization__c == 'Deshkal'){
empId.add(sd.Employee_Information__c);
}
}
system.debug(' empIdt' + empId);
}
for(Employee_Information__c emp : [select id,Name From Employee_Information__c where id IN :empId]){
for(D_Transaction__c dt : [SELECT RecordTypeId,D_Amount__c,D_Employee__c,D_Advance_Status__c,D_Advance_Repay__c,D_Name__c FROM D_Transaction__c where D_Employee__c =:emp.Id AND RecordTypeId =:expense AND D_Advance_Status__c = 'Open' ]){
D_Transaction__c d = new D_Transaction__c();
d.D_Employee__c = emp.Id;
d.RecordTypeId = income;
d.D_Transaction__c =dt.Id;
Integer result = integer.valueOf(dt.D_Amount__c) / integer.valueOf(dt.D_Advance_Repay__c);
d.D_Amount__c = result;
system.debug('d.D_Amount__c');
dList.add(d);
}
}
if(!dList.isEmpty()){
insert dList;
}
}
}
Test Class:
@isTest
private class InsertIncomeRecordHandlerTest {
@isTest
static void testInsertRecord() {
// Create test data
Employee_Information__c emp = new Employee_Information__c();
emp.Salutation__c = 'Mr.';
emp.Name = 'Test';
emp.Employee_Last_Name__c = 'HR Admin';
emp.Date_of_Join__c = system.today();
emp.Gender__c = 'Male';
emp.Personal_Email__c = 'abc@gmail.com';
emp.Marital_Status__c = 'Single';
emp.Employment_Status__c = 'Active';
emp.DN_Designation__c='CEO';
emp.Date_of_Birth__c=Date.newInstance(2000, 12, 9);
emp.Gender__c='Male';
emp.Manager_Supervisor__c = 'Deepu H D';
emp.Work_Location__c = 'NOC';
emp.Bank_Name__c='HDFC';
emp.Account_Number__c='12345678901234';
emp.Mobile_Number__c='9999999999';
emp.IFSC_CODE__c='HDFC0000003';
emp.Confirmed_CTC__c=800000;
emp.Basic_Percentage__c='40';
emp.Tax_Regime__c='Old Regime';
emp.PFs__c='Percentage';
emp.Marital_Status__c='Single';
emp.Aadhaar_Number__c='265385644663';
emp.Address__c='Test';
emp.City__c='Chennai';
emp.Organization__c='Deshkal';
emp.Postal_Code__c='600001';
emp.DN_Leave_Process__c='Yes';
insert emp;
Salary_Detail__c salary = new Salary_Detail__c(
Employee_Information__c = emp.Id,
RecordTypeId = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('Transaction').getRecordTypeId(),
Advance__c = 60,
Isdetected__c = false,
Organization__c = 'Deshkal'
);
insert salary;
D_Transaction__c dt = new D_Transaction__c ();
dt.D_Employee__c = emp.Id;
dt.D_Amount__c = 100;
dt.D_Advance_Repay__c =' 12';
dt.RecordTypeId = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('expense').getRecordTypeId();
dt.D_Advance_Status__c = 'Open';
Test.startTest();
insert dt;
Test.stopTest();
}
}
The issue in the test class is that it does not pass any parameters to the method InsertIncomeRecordHandler.insertRecord(). As a result, the variables salList and oldMap in the method are not initialized, which leads to a NullPointerException error.
Try with below test class :
If the above information helps , please mark this as Best Answer.
Thank you.
public class InsertIncomeRecordHandler {
public static void insertRecord(List<Salary_Detail__c> salList,Map<id,Salary_Detail__c> oldMap){
Set<Id> empId = new Set<Id>();
List<D_Transaction__c> dList = new List<D_Transaction__c>();
ID income = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Income').getRecordTypeId();
ID trans = Schema.SObjectType.Salary_Detail__c.getRecordTypeInfosByName().get('Transaction').getRecordTypeId();
ID expense = Schema.SObjectType.D_Transaction__c.getRecordTypeInfosByName().get('Expense').getRecordTypeId();
for(Salary_Detail__c sd : salList){
if(sd.Advance__c != oldMap.get(sd.Id).Advance__c && sd.Isdetected__c !=oldMap.get(sd.Id).Isdetected__c){
if(sd.Advance__c >0 && sd.RecordTypeId == trans && sd.Organization__c == 'Deshkal'){
empId.add(sd.Employee_Information__c);
}
}
system.debug(' empIdt' + empId);
}
for(Employee_Information__c emp : [select id,Name From Employee_Information__c where id IN :empId]){
for(D_Transaction__c dt : [SELECT RecordTypeId,D_Amount__c,D_Employee__c,D_Advance_Status__c,D_Advance_Repay__c,D_Name__c FROM D_Transaction__c where D_Employee__c =:emp.Id AND RecordTypeId =:expense AND D_Advance_Status__c = 'Open' ]){
D_Transaction__c d = new D_Transaction__c();
d.D_Employee__c = emp.Id;
d.RecordTypeId = income;
d.D_Transaction__c =dt.Id;
Decimal result = Integer.valueOf(dt.D_Amount__c) / Integer.valueOf(dt.D_Advance_Repay__c);
system.debug('result' +result);
d.D_Amount__c = result;
system.debug('d.D_Amount__c');
dList.add(d);
}
}
if(!dList.isEmpty()){
insert dList;
}
}
}