+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

Trigger error- Anybody can help

Hi Expert,

Have to update the field 'actual hours' of "project Portfolio" object through all the roll up of the 'actual hours' of "project" object. 

iam getting an error- System.NullPointerException: Attempt to de-reference a null object: Class.updatedActualHours.processAfterUpdateActualHours: line 61, column 1

here is the code-

public class updatedActualHours 

{
  public static List<CloudbyzITPM__Project__c> affectedActualHours = new List<CloudbyzITPM__Project__c>();
    public static List<CloudbyzITPM__Project__c> oldaffectedActualHours = new List<CloudbyzITPM__Project__c>();
  public static set<ID> affc = new set<ID>();
    public static set<ID> affcold = new set<ID>();
  public static void processAfterUpdateActualHours()
 {
     affectedActualHours = (List<CloudbyzITPM__Project__c>)Trigger.New;
     oldaffectedActualHours = (List<CloudbyzITPM__Project__c>)Trigger.Old;
     if(Trigger.isDelete){
        for(CloudbyzITPM__Project__c  affc1 : oldaffectedActualHours)
     {
         if(affc1.CloudbyzITPM__Project_Portfolio__c != null)
         {
         system.debug('value of affc1'+ affc1);
        affc.add(affc1.CloudbyzITPM__Project_Portfolio__c);
     }
     }
      system.debug('this is the value'+ affc);
    CloudbyzITPM__Project_Portfolio__c fn = [select id,name,CloudbyzITPM__Actual_Hours__c from CloudbyzITPM__Project_Portfolio__c where id in: affc limit 1];
    list<CloudbyzITPM__Project__c> affectedActualHours1 = [select id, name,CloudbyzITPM__Actual_Hours__c, CloudbyzITPM__Project_Portfolio__c from CloudByzITPM__Project__c where CloudbyzITPM__Project_Portfolio__c = :fn.id];
          
      Decimal j=0;
     System.debug('@@affectedActualHours1@@'+affectedActualHours1+'@@affectedActualHours1.size'+affectedActualHours1.size());  
    if(affectedActualHours1.size()>0)
       {
           for(CloudbyzITPM__Project__C afc1 : affectedActualHours1)
        {
            System.debug('@@afc1.CloudbyzITPM__Actual_Hours__c@@'+afc1.CloudbyzITPM__Actual_Hours__c);
          if(afc1.CloudbyzITPM__Actual_Hours__c !=0 && afc1.CloudbyzITPM__Actual_Hours__c!=null)
            j = j + afc1.CloudbyzITPM__Actual_Hours__c;
        }
    }
    
 if(j>0)
 {
     System.debug('@@pBUFinal@@');
    fn.CloudbyzITPM__Actual_Hours__c = j;
    }
          else{
              fn.CloudbyzITPM__Actual_Hours__c = 0;
          }
     update fn; 
      System.debug('@@fn4@@'+fn);
    }
    else{
     if(affectedActualHours.size()>0)
     {/*
     for(CloudbyzITPM__Project__c  affc1 : affectedActualHours)
     {
         if(affc1.CloudbyzITPM__Project_Portfolio__c != null)
         {
         system.debug('value of affc1'+ affc1);
        affc.add(affc1.CloudbyzITPM__Project_Portfolio__c);
     }
     }*/
         system.debug('@@@value of affc1'+ affectedActualHours);
         for(integer i = 0; i<affectedActualHours.size(); ++i){
         if(affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != null && oldaffectedActualHours[i]!= null && oldaffectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != null && oldaffectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c)
         {
         system.debug('value of affc1'+ affectedActualHours[i]);
        affc.add(affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c);
             affcold.add(oldaffectedActualHours[i].CloudbyzITPM__Project_Portfolio__c);
         }
             if(affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != null)
         {
         system.debug('value of affc1'+ affectedActualHours[i]);
        affc.add(affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c);
         } 
             else if(affectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != null && oldaffectedActualHours[i]!= null && oldaffectedActualHours[i].CloudbyzITPM__Project_Portfolio__c != null){
                affc.add(oldaffectedActualHours[i].CloudbyzITPM__Project_Portfolio__c); 
             }
             
         }
          if(affcold.size()>0)
      {
     system.debug('this is the value'+ affc);
    CloudbyzITPM__Project_Portfolio__c fn = [select id,name,CloudbyzITPM__Actual_Hours__c from CloudbyzITPM__Project_Portfolio__c where id in: affcold limit 1];
    list<CloudbyzITPM__Project__c> affectedActualHours1 = [select id, name,CloudbyzITPM__Actual_Hours__c, CloudbyzITPM__Project_Portfolio__c from CloudByzITPM__Project__c where CloudbyzITPM__Project_Portfolio__c = :fn.id];
      
      Decimal j=0;
     System.debug('@@affectedActualHours1@@'+affectedActualHours1+'@@affectedActualHours1.size'+affectedActualHours1.size());  
    if(affectedActualHours1.size()>0)
       {
           for(CloudbyzITPM__Project__C afc1 : affectedActualHours1)
        {
            System.debug('@@afc1.CloudbyzITPM__Actual_Hours__c@@'+afc1.CloudbyzITPM__Actual_Hours__c);
          if(afc1.CloudbyzITPM__Actual_Hours__c !=0 && afc1.CloudbyzITPM__Actual_Hours__c!=null)
            j = j + afc1.CloudbyzITPM__Actual_Hours__c;
        }
    }
    
 if(j>0)
 {
     System.debug('@@pBUFinal@@');
    fn.CloudbyzITPM__Actual_Hours__c = j;
    }
          else{
              fn.CloudbyzITPM__Actual_Hours__c = 0;
          }
     update fn; 
      System.debug('@@fn4@@'+fn);
    }
      if(affc.size()>0)
      {
     system.debug('this is the value'+ affc);
    CloudbyzITPM__Project_Portfolio__c fn = [select id,name,CloudbyzITPM__Actual_Hours__c from CloudbyzITPM__Project_Portfolio__c where id in: affc limit 1];
    list<CloudbyzITPM__Project__c> affectedActualHours1 = [select id, name,CloudbyzITPM__Actual_Hours__c, CloudbyzITPM__Project_Portfolio__c from CloudByzITPM__Project__c where CloudbyzITPM__Project_Portfolio__c = :fn.id];
      
      Decimal j=0;
     System.debug('@@affectedActualHours1@@'+affectedActualHours1+'@@affectedActualHours1.size'+affectedActualHours1.size());  
    if(affectedActualHours1.size()>0)
       {
           for(CloudbyzITPM__Project__C afc1 : affectedActualHours1)
        {
            System.debug('@@afc1.CloudbyzITPM__Actual_Hours__c@@'+afc1.CloudbyzITPM__Actual_Hours__c);
          if(afc1.CloudbyzITPM__Actual_Hours__c !=0 && afc1.CloudbyzITPM__Actual_Hours__c!=null)
            j = j + afc1.CloudbyzITPM__Actual_Hours__c;
        }
    }
    
 if(j>0)
 {
     System.debug('@@pBUFinal@@');
    fn.CloudbyzITPM__Actual_Hours__c = j;
    }
          else{
              fn.CloudbyzITPM__Actual_Hours__c = 0;
          }
     update fn; 
      System.debug('@@fn4@@'+fn);
    }
    }
    }
}
}User-added image
Deepak Sharma 184Deepak Sharma 184
Yes, it is in line 61, i have updated the trigger with the null check in the if condition 'affectedActualHours[i]!= null', it was working fine, now it is giving the same error.

Note- I have 8 triggers on the same object.