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
defray userdefray user 

emiTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.emiTrigger: line 36, column 1

trigger emiTrigger on EMI_Payment__c (after insert) {
      Map<ID, Loans__c> parentLoans = new Map<ID, Loans__c>(); 
      List<id> listIds = new List<id>();
      Double Principal_Outstanding_at_the_end_of_the_month;
            
      for(EMI_Payment__c e : Trigger.new){
      listIds.add(e.Loan_Id__c);//getting EMI ID(s)
      }
   //getting parent record ID and data with above ID
   parentLoans = new Map<Id,Loans__c>([SELECT id,paid_amount__c,Loan_Amount__c,TenureYrs__c,
   Interest_Rate__c,Outstanding_Principal__c,(SELECT id,amount__c from
   EMI_Payment__r) from Loans__c  where ID IN : listIds ]);  
   //updating paid amount with EMI 
   for(EMI_Payment__c e:Trigger.new){
         Loans__c myloan = parentLoans.get(e.Loan_Id__c);
         
        Double  principal = myloan.Loan_Amount__c;
        Decimal tenure = myloan.TenureYrs__c*12;
        Decimal interest = myloan.Interest_Rate__c/12/100;
        Decimal pr = 1 + interest;
        Double EMI =  principal * interest * Math.pow(pr.doubleValue(),tenure.doubleValue())/(Math.pow(pr.doubleValue(),tenure.doubleValue())-1);
      
            Double Interest_Component_of_EMI = interest * myloan.Outstanding_Principal__c;
            Double Principal_Component_of_EMI = Math.round(EMI - Interest_Component_of_EMI);
            Principal_Outstanding_at_the_end_of_the_month = myloan.Outstanding_Principal__c - Principal_Component_of_EMI;
        
            myloan.Outstanding_Principal__c-= Principal_Component_of_EMI;//decrementing outstading balance
            myloan.paid_amount__c += e.amount__c; //incrementing paid amount
   }
   
   if(parentLoans.size()>0){
   upsert parentLoans.values();  // error occurring here
   }
}
Anthony McDougaldAnthony McDougald
Hello Defray,
Hope that your day is off to an amazing start. We've assessed your code and hope it helps. Please test and report back to us when you get the chance. Hope this helps and may God bless you abundantly.
trigger emiTrigger on EMI_Payment__c (after insert) {
    Map<ID, Loans__c> parentLoans = new Map<ID, Loans__c>(); 
    List<id> listIds = new List<id>();
	List<Loans__c> updatedLoans = new List<Loans__c>();
    Double Principal_Outstanding_at_the_end_of_the_month;
            
    for(EMI_Payment__c e : Trigger.new){
		listIds.add(e.Loan_Id__c);//getting EMI ID(s)
    }
	//getting parent record ID and data with above ID
    parentLoans = new Map<Id,Loans__c>([SELECT id,paid_amount__c,Loan_Amount__c,TenureYrs__c,
    Interest_Rate__c,Outstanding_Principal__c,(SELECT id,amount__c from
    EMI_Payment__r) from Loans__c  where ID IN : listIds ]);  
    //updating paid amount with EMI 
    for(EMI_Payment__c e:Trigger.new){
        Loans__c myloan = parentLoans.get(e.Loan_Id__c);
    
        Double  principal = myloan.Loan_Amount__c;
        Decimal tenure = myloan.TenureYrs__c*12;
        Decimal interest = myloan.Interest_Rate__c/12/100;
        Decimal pr = 1 + interest;
        Double EMI =  principal * interest * Math.pow(pr.doubleValue(),tenure.doubleValue())/(Math.pow(pr.doubleValue(),tenure.doubleValue())-1);
      
        Double Interest_Component_of_EMI = interest * myloan.Outstanding_Principal__c;
        Double Principal_Component_of_EMI = Math.round(EMI - Interest_Component_of_EMI);
        Principal_Outstanding_at_the_end_of_the_month = myloan.Outstanding_Principal__c - Principal_Component_of_EMI;
        
        myloan.Outstanding_Principal__c-= Principal_Component_of_EMI;//decrementing outstading balance
        myloan.paid_amount__c += e.amount__c; //incrementing paid amount
		
		updatedLoans.add(myloan); //add loans to a list to be commited to the database
   }
   
   if(updatedLoans.size() > 0){
   upsert updatedLoans;
   }
}


Best Regards,
Anthony McDougald