+ Start a Discussion

argument cannot be null - error - trigger

Hi, I have an error in my trigger and I'm not sure why I get it... It is like this: BeforeUpdate cause by: System.DMLException; Upsert field. FIeld Exeception on row 0; first error; CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Execution of AfterUpdate cause by: System.NullPointerException: Argument cannot be null();


This is the snippet code where the error is happening....

List<Id> woIds = new List<Id>();
        List<DateTime> listDateTimesValues = new List<DateTime>();
        List<DateTime> listDateTimesValues2 = new List<DateTime>();
        for(SVMXC__Service_Order_Line__c storeWo : newMap.values()){
        //store all the work details related to the Work Order
        List<SVMXC__Service_Order_Line__c> relatedWorkDetails = [SELECT ID, SIG_Start_Date_Time_Device__c , SIG_End_Date_Time_Device__c from SVMXC__Service_Order_Line__c where SVMXC__Service_Order__c =: woIds]; 
        //fetch the current Work Order
        List<SVMXC__Service_Order__c> currentWorkOrder = [SELECT ID, Start_of_Work_c__c, End_of_Work__c from SVMXC__Service_Order__c where ID =: woIds];
        if(relatedWorkDetails.size() >0 ){
        for(SVMXC__Service_Order_Line__c workDetail : relatedWorkDetails ){
          DateTime dt = DateTime.valueOfGMT(workDetail.SIG_Start_Date_Time_Device__c);
          DateTime dt2 = DateTime.valueOfGMT(workDetail.SIG_End_Date_Time_Device__c);
        //check how many items are in the list
        Integer nrOfItemsInTheList = listDateTimesValues.size() -1;
        Integer nrOfItemsInTheList2 = listDateTimesValues2.size() -1;
          DateTime minDateTime = listDateTimesValues[0];
          DateTime maxDateTime = listDateTimesValues2[nrOfItemsInTheList];
          System.debug('minDateTime '+minDateTime );
          System.debug('maxDateTime '+maxDateTime );
         //convert the minDateTime and maxDateTime in a text to avoid time Conversion
          String stringMinDateTime = String.valueOfGmt(minDateTime);
          String stringMaxDateTime = String.valueOfGmt(maxDateTime);
          System.debug('stringMinDateTime '+ stringMinDateTime );
          System.debug('stringMaxDateTime '+ stringMaxDateTime );
          List<SVMXC__Service_Order__c> woToUpdate = new List<SVMXC__Service_Order__c>();
          for(SVMXC__Service_Order__c workOrder : currentWorkOrder) {
              workOrder.Start_of_Work_c__c= stringMinDateTime;
              workOrder.End_of_Work__c = stringMaxDateTime;
            update woToUpdate;

What I'm doing wrong here?
AnudeepAnudeep (Salesforce Developers) 
Most of the time doing a size check fixes null pointer issues. I recommend doing a size check similar to relatedWorkDetails for listDateTimesValues2 and listDateTimesValues as well before trying to access the list 
if(relatedWorkDetails.size() >0 ){

I am suspecting the following lines are throwing an error
DateTime minDateTime = listDateTimesValues[0];          
 DateTime maxDateTime = listDateTimesValues2[nrOfItemsInTheList];

I recommend reviewing the help article -
NullPointerException de-reference a null object in Apex code trigger

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!