I'm getting the following error when attempting to mass update accounts via Data Loader. The id throwing the error is the same in each instance. The Trigger (listed below) is meant to aggregate field ApexHours__c from child accounts, combine with the value of the parent and populate Total_Hours__c. The account that's triggering the error has a parent, while I've successfully updated those w/o parent accounts. So I'm assuming I'm referencing a null value somewhere but can't find it.
"TIParentRollUp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00130000005o3WCAAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TIParentRollUp: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TIParentRollUp: line 9, column 1: [] Trigger.TIParentRollUp: line 20, column 1"
Trigger:
trigger TIParentRollUp on Account (after insert, before update, after update, after delete, after undelete) {
if(Trigger.isBefore) {
for(account a:trigger.new)
if(a.ApexHours__c ==null)
a.ApexHours__c =0;
else
a.Total_Hours__c=a.ApexHours__c ;
for(aggregateresult a:[select parentid,sum(ApexHours__c )sumb from account where ApexHours__c <>null and parentid in :trigger.new group by parentid])
trigger.newmap.get((id)a.get('parentid')).Total_Hours__c += (decimal)a.get('sumb');
} else {
set<id> parents = new set<id>();
if(trigger.old!=null)
for(account a:trigger.old)
parents.add(a.parentid);
if(trigger.new!=null)
for(account a:trigger.new)
parents.add(a.parentid);
parents.remove(null);
for(account[] aa:[select id from account where id in :parents])
update aa;
}
}