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
VICKY_SFDCVICKY_SFDC 

null pointer Exception In Trigger

employee==>API:  junebatch2021__Employee__c

employee payment==>API: junebatch2021__EMP_PAYMENT__c

Both in Lookup r/s where employee is parent.

Now,
employee payment there is a amount field soas many time i enter amount corresponding to there employee it will rollup and Relect
the SUM IN 

Total field :API==>junebatch2021__Total__c
WHICH IS CURRENCY FIELD ON PARENT OBJECT WITH DEFAULT VALUE 0.


I AM POSTING ERROR AND BELOW THAT MY HANDLER AND TRIGGER,,,
IF ANY ONE CAN GIVE SOME VALUABLE FEEDBACK ,,THAT WOULD BE VERY HELPFULL.


Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger junebatch2021.deleteAction caused an unexpected exception, contact your administrator: junebatch2021.deleteAction: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.junebatch2021.deleteAction.del: line 3, column 1





Handler:

public class EmpLaymentHelper {
    public static void beforeInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
        for(junebatch2021__EMP_PAYMENT__c p:payments){
            if(p.junebatch2021__Employee__c==null){
                p.junebatch2021__Employee__c.addError('SELECT THE EMPLOYEE');
            }
        }
    }
    public static void afterInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
        Map<Id,List<junebatch2021__EMP_PAYMENT__c>> payMap=new Map<Id,List<junebatch2021__EMP_PAYMENT__c>>();
        try{
        for(junebatch2021__EMP_PAYMENT__c p:payments){
            ID empId=p.junebatch2021__Employee__c;
           Boolean flag=payMap.containsKey(empId);
           List<junebatch2021__EMP_PAYMENT__c> payList=new List<junebatch2021__EMP_PAYMENT__c>();
            
            if(flag==True){
              payList=payMap.get(empId);
              payList.add(p);
              payMap.put(empId,payList);  
            }else{
                payList.add(p);
              payMap.put(empId,payList);   
            }
        }
        
        //LOGIC OF SUM IN CHILD
        
       Map<Id,Decimal> empMap=new Map<Id,Decimal>();
        for(Id key:payMap.keySet()){
            List<junebatch2021__EMP_PAYMENT__c> pays=payMap.get(key);
            Decimal sum=0;
            for(junebatch2021__EMP_PAYMENT__c pa:pays){
                sum=sum+pa.junebatch2021__EMP_AMOUNT__c;
            }
            empMap.put(key,sum);
        }
//NOW REFLECT THAT UPDATE IN PARENT
        
List<junebatch2021__Employee__c> employees=[Select id,junebatch2021__Total__c from junebatch2021__Employee__c where id in:empMap.keySet()];        
        for(junebatch2021__Employee__c e:employees){
          e.junebatch2021__Total__c=e.junebatch2021__Total__c+empMap.get(e.Id);  
        }       
update employees;
    } 
         catch(Exception e){
        e.getMessage();
    }
}
    
   
}



Trigger:

trigger EmpLaymentHelper on junebatch2021__EMP_PAYMENT__c (before insert,after insert,after update,after delete) {
    if(Trigger.isBefore && Trigger.isInsert){
        EmpLaymentHelper.beforeInsert(Trigger.new);
    }
    else{
        if(Trigger.isAfter && Trigger.isInsert){
            EmpLaymentHelper.afterInsert(Trigger.new);
        }
    }
}
mukesh guptamukesh gupta
Hi Vikas,

Please use below code:-
 
Handler:

public class EmpLaymentHelper {
    public static void beforeInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
        for(junebatch2021__EMP_PAYMENT__c p:payments){
            if(p.junebatch2021__Employee__c==null){
                p.junebatch2021__Employee__c.addError('SELECT THE EMPLOYEE');
            }
        }
    }
    public static void afterInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
        Map<Id,List<junebatch2021__EMP_PAYMENT__c>> payMap=new Map<Id,List<junebatch2021__EMP_PAYMENT__c>>();
        try{
        for(junebatch2021__EMP_PAYMENT__c p:payments){
            ID empId=p.junebatch2021__Employee__c;
			if(!payMap.isEmpty() && payMap.size() > 0){
			
           Boolean flag=payMap.containsKey(empId);
           List<junebatch2021__EMP_PAYMENT__c> payList=new List<junebatch2021__EMP_PAYMENT__c>();
            
            if(flag==True){
              payList=payMap.get(empId);
              payList.add(p);
              payMap.put(empId,payList);  
            }else{
                payList.add(p);
              payMap.put(empId,payList);   
            }
        }
        
		}
        //LOGIC OF SUM IN CHILD
        
       Map<Id,Decimal> empMap=new Map<Id,Decimal>();
        for(Id key:payMap.keySet()){
            List<junebatch2021__EMP_PAYMENT__c> pays=payMap.get(key);
            Decimal sum=0;
            for(junebatch2021__EMP_PAYMENT__c pa:pays){
                sum=sum+pa.junebatch2021__EMP_AMOUNT__c;
            }
            empMap.put(key,sum);
        }
//NOW REFLECT THAT UPDATE IN PARENT
        
List<junebatch2021__Employee__c> employees=[Select id,junebatch2021__Total__c from junebatch2021__Employee__c where id in:empMap.keySet()];        
        for(junebatch2021__Employee__c e:employees){
          e.junebatch2021__Total__c=e.junebatch2021__Total__c+empMap.get(e.Id);  
        }       
update employees;
    } 
         catch(Exception e){
        e.getMessage();
    }
}
    
   
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
VICKY_SFDCVICKY_SFDC
@Mukesh Gupta ,,Thanks for reply but still getting this Error

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger junebatch2021.deleteAction caused an unexpected exception, contact your administrator: junebatch2021.deleteAction: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.junebatch2021.deleteAction.del: line 3, column 1
 
mukesh guptamukesh gupta
Hi Vikas,

Can you share the code of  junebatch2021.deleteAction.del: line 3

 
VICKY_SFDCVICKY_SFDC
@Manish

Handler:

public class deleteAction {
    public static void del(Map<Id,junebatch2021__Employee__c> empMap){
        Set<Id> empId=empMap.keyset();//STORE ID's
        List<junebatch2021__EMP_PAYMENT__c> payments=[select id from junebatch2021__EMP_PAYMENT__c where id in: empId];
    delete payments;
    }
}

Trigger:

trigger deleteAction on junebatch2021__Employee__c (Before insert) {
deleteAction.del(Trigger.oldMap);
}
mukesh guptamukesh gupta
Hi Vikas,

Please use below code:-
Handler:

public class deleteAction {
    public static void del(Map<Id,junebatch2021__Employee__c> empMap){
 if(!empMap.isEmpty() && empMap.size() > 0){
        Set<Id> empId=empMap.keyset();//STORE ID's
        List<junebatch2021__EMP_PAYMENT__c> payments=[select id from junebatch2021__EMP_PAYMENT__c where id in: empId];
    delete payments;
}
    }
}

Trigger:

trigger deleteAction on junebatch2021__Employee__c (Before insert) {
deleteAction.del(Trigger.oldMap);
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
VICKY_SFDCVICKY_SFDC
THanks for the Code,,,Bt Its was throwing the Error ,,,,so i deactivated "deleteAction " trigger,,,And then No Error On wards,,


Bt the issues is sum of AMOUNT on Child Object Is Not Reflecting On the Total field in Parent Object
mukesh guptamukesh gupta
HI Vikas,

 are you getting this error again System.NullPointerException: Attempt to de-reference a null object
VICKY_SFDCVICKY_SFDC
@Mukhesh  No,But Stll the  roll up of child amount not reflecting On parent.
mukesh guptamukesh gupta
ok,

But i put my efforts on your error and it's solved, so  you should mark as the best answer.

and if you have different issue the please create a new question