+ Start a Discussion
PaulATPimptastiPaulATPimptasti 

Null Reference After Looping Through Same Object

I have a strange one here.  I am looping through the same object twice within a method.  The first time I iterate through the object it works, yet the second time I get a null pointer exception error.

 

 

//sum the case time global Case[] CalculateCaseTime(Case[] cases, for__Timesheet__c[] caseTimesheets) { try { if(cases.size() > 0 && caseTimesheets.size() >0) { //this zeros the Case's billable hours so as to prevent the values present on the Case //from being summed as this will create incorrect quantities. for(Case currentCase:cases) { for(for__Timesheet__c caseTimesheet:caseTimesheets) { caseTimesheet.for__Case__r.for__Billable_Hours__c = 0; system.debug(' - Zeoring Timesheet\'s Case Billable Hours = ' + caseTimesheet.for__Case__r.for__Billable_Hours__c); } } system.debug(cases); for(Case currentCase:cases) { system.debug('Calculating the total amount of time spent on Case ' + currentCase.Id); currentCase.for__Hours_Worked__c = 0; system.debug(' - Zeroing Case\'s Hours Worked = ' + currentCase.for__Hours_Worked__c); currentCase.for__Billable_Hours__c = 0; system.debug(' - Zeroing Case\'s Billable Hours Worked = ' + currentCase.for__Billable_Hours__c); for(for__Timesheet__c caseTimesheet:caseTimesheets) { if(currentCase.Id == caseTimesheet.for__Case__c) { currentCase.for__Hours_Worked__c += caseTimesheet.for__Activity_Duration__c; currentCase.for__Billable_Hours__c += caseTimesheet.for__Total_Billable_Hours__c; caseTimesheet.for__Case__r.for__Billable_Hours__c += caseTimesheet.for__Total_Billable_Hours__c; system.debug(' - Timesheet\'s Case Billable Hours = ' + caseTimesheet.for__Case__r.for__Billable_Hours__c); } } } } return cases; } catch(exception excpt) { system.debug('CalculateCaseTime exception = ' +excpt); f.debug('CalculateCaseTime exception Cause = ' + excpt.getCause()); f.debug('CalculateCaseTime exception Message = ' + excpt.getMessage()); f.debug('CalculateCaseTime exception Name = ' + excpt.getTypeName()); processedOK = false; return null; } }

 

 When the cases has one record I am able to get here:

 

caseTimesheet.for__Case__r.for__Billable_Hours__c = 0; system.debug(' - Zeoring Timesheet\'s Case Billable Hours = ' + caseTimesheet.for__Case__r.for__Billable_Hours__c);

 Yet when it tries to execute this:

 

 

system.debug(cases);

  Or loop through the cases variable for a second time I get this error:

 

 

CalculateCaseTime exception = System.NullPointerException: Attempt to de-reference a null object

 Please help as it has only stopped working recently and I haven't change that code so I suspect it could be an issue with Sf.com but not 100% sure