+ Start a Discussion
kandy11kandy11 

Null Point Exception

 

Can some please review this and iam getting an null point exception at the line which is marked in red.

 

public List<Voucher__c> getRelatedVoucher() {

NumberEventsBenefiting = 0;
TotalRaffles = 0;
TotalEvents = 0;
RelatedVoucher = new List<Voucher__c>();
RelatedTreasureChestApplications = new List<Treasure_Chest_Application__c>();
this.Voucherid = 'a1Ac0000000VaCi';
if(Voucherid != null && Voucherid!= ''){


for(Voucher__c queryvouchers : [select (select Amount_Raised_for_Event__c,CreatedDate,Stage__c, Date_Accepted__c,
Amount_Raised_for_Voucher__c, Location_of_event__c,Name_of_the_Event__c,
Date_of_the_Event__c,Temporary_Voucher__r.Account__r.Name
from Treasure_Chest_Applications__r )
Total_Amount_Raised_for_Events__c,Total_Amount_Raised_for_Vouchers__c,Number_of_Events_Benefiting__c,Term__c,
Name,CreatedDate from Voucher__c where id=: Voucherid ]){

for(Treasure_Chest_Application__c TCA :queryvouchers.Treasure_Chest_Applications__r){

if((date.today().month() - TCA.Date_of_the_Event__c.month() == 1) && (date.today().year() == TCA.Date_of_the_Event__c.year())){

RelatedVoucher.add(queryvouchers);
}

}
}
for(Voucher__c CalVouchers : RelatedVoucher ){
NumberEventsBenefiting += Integer.valueOf(CalVouchers.Number_of_Events_Benefiting__c);
TotalRaffles += Integer.valueOf(CalVouchers.Total_Amount_Raised_for_Vouchers__c);
TotalEvents += Integer.valueOf(CalVouchers.Total_Amount_Raised_for_Events__c);
}


}
return RelatedVoucher;
}

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

try this,

 

for(Treasure_Chest_Application__c TCA :queryvouchers.Treasure_Chest_Applications__r){
  if(TCA.Date_of_the_Event__c != null){
    if((date.today().month() - TCA.Date_of_the_Event__c.month() == 1) && (date.today().year() == TCA.Date_of_the_Event__c.year())){
	  RelatedVoucher.add(queryvouchers);
	}
  }
}

 

All Answers

Dhaval PanchalDhaval Panchal
Check null before accessing field TCA.Date_of_the_Event__c.
kandy11kandy11
Hi Dhaval

How do i check the null before accessing?
Dhaval PanchalDhaval Panchal

try this,

 

for(Treasure_Chest_Application__c TCA :queryvouchers.Treasure_Chest_Applications__r){
  if(TCA.Date_of_the_Event__c != null){
    if((date.today().month() - TCA.Date_of_the_Event__c.month() == 1) && (date.today().year() == TCA.Date_of_the_Event__c.year())){
	  RelatedVoucher.add(queryvouchers);
	}
  }
}

 

This was selected as the best answer
kandy11kandy11
thanks dhaval for the solution provided....