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
Molson94Molson94 

Help Needed: System.NullPointerException: Attempt to de-reference a null object

Hello,

Banging my head against the wall trying to figure out why "System.NullPointerException: Attempt to de-reference a null object" error is occuring on an After Update trigger. For the 200 records that are not being updated because of this trigger's error, i am stumped because those records do not meet the criteria in the initial for loop (line 7) to even be included in the list of recrods i am targeting. Does anyone know why this error is being thrown on line 68 (bolded).

Thanks in advance.
 
trigger PrintJobCreate on Title__c (after update) {
    List<Title__c> ProductionTitles = new List<Title__c>();
    List<masterID__c> masterIDs = new List<masterID__c>();
    List<Print_Job__c> NewJobs = new List<Print_Job__c>();
    List<Vendor__c> printer = new List<Vendor__c>([SELECT id FROM Vendor__c WHERE Name = 'Printer' LIMIT 1]);
    
    for (Title__c t : Trigger.new) { //all titles that were updated
        if (t.status__c != Trigger.oldMap.get(t.Id).status__c) { //only look at titles that had a status change
            if (t.Status__c == 'Production' || t.Status__c == 'Art In Progress') { //isolate titles that have moved into production or art creation
                ProductionTitles.add(t);
            }
            
        } 
    }
    if (!ProductionTitles.isEmpty()) {
        //Get current list of masterIDs to avoid duplicates
        //compare masterIDs in OpenJobs with masterIDs in ProductionTitles
        system.debug(ProductionTitles);
        Set<String> OJ = new Set<String>();
        Set<Print_job__c> OpenJobs = new Set<Print_Job__C>([SELECT masterID_Format__c FROM Print_Job__c WHERE Print_Job_Status__c != 'Complete']);
        
        Set<String> OA = new Set<String>();
        //
        Set<masterID__c> OpenmasterIDs = new Set<masterID__c>([SELECT id, Name FROM masterID__c WHERE Name NOT IN ('Digital', 'Digital Audio', 'Physical Audio') AND x3p__c = false  AND Title__r.id IN:ProductionTitles]);
        //Grab all masterID Ids of all open jobs
        for(Print_Job__c OJS : OpenJobs){
            OJ.add(String.valueOf(OJS.masterID_Format__c));
            //system.debug('Open Print Job '+ OJ);
        }
        
        //Grab all masterID Ids of triggered masterIDs that meet print criteria
        for(masterID__c OAS : OpenmasterIDs){
            OA.add(String.valueOf(OAS.Id));
            system.debug('masterID Matching Criteria: '+ OA);
            
        }
        
        //Remove current open jobs from list of masterIDs that meet criteria
        for(String job : OJ){
            if (OA.contains(job)){
                OA.remove(job);
                system.debug('Remaining DeDuped masterIDS ' +OA);
            }
        }
        
        //Requery the list for the 
        masterIDs = [SELECT id, Name, masterID__c, x3p__c, Color_BW_Interior__c, pub_Publish_Date__c, title__r.id, title__r.internal_imprint__c 
                 FROM masterID__c 
                 WHERE
                 id IN :OA ];
        system.debug(masterIDs);
    }
    if (!masterIDs.isEmpty()) {
        for (masterID__c a : masterIDs) {
            Print_Job__c pj = new Print_Job__c();
            
            pj.Title__c = a.Title__c;
            pj.masterID_Format__c = a.Id;
            pj.Job_Type__c = 'Initial Print Run';
            pj.Print_Job_Status__c = 'Pending';
            pj.Printer__c = printer.get(0).id;
            
            if(a.Color_BW_Interior__c == '1/1' && a.Name == 'Paperback'){ //standard pub paperback
                //standard location
                pj.Print_Location__c = 'Printer US';
                //standard schedule
                pj.Units_Spec_Due_Date__c = a.pub_Publish_Date__c - 56;
                pj.Files_Due_At_Printer__c = a.
                    _Publish_Date__c - 56;
                pj.Estimated_Ship__c = a.pub_Publish_Date__c - 35;
                pj.BPI_Forecast_Due__c = pj.Units_Spec_Due_Date__c - 7;
                
                pj.Standard_CPU__c = true;
                NewJobs.add(pj);
            }else if(a.Color_BW_Interior__c == '1/1' && a.Name == 'Hardcover'){ //1C Hardcover
                //standard location
                pj.Print_Location__c = 'Printer US';
                //standard hc schedule
                pj.Units_Spec_Due_Date__c = a.pub_Publish_Date__c - 84;
                pj.Files_Due_At_Printer__c = a.pub_Publish_Date__c - 63;
                pj.Estimated_Ship__c = a.pub_Publish_Date__c - 35;
                pj.BPI_Forecast_Due__c = pj.Units_Spec_Due_Date__c - 7;
                
                pj.Standard_CPU__c = false;
                pj.RFQ_Required__c = true;
                NewJobs.add(pj);
            }else if(a.Color_BW_Interior__c != '1/1' && a.Color_BW_Interior__c != '' && a.Name == 'Hardcover' && a.Title__r.Internal_imprint__c == 'Two Lions'){ //TL Hardcover
                //standard location
                pj.Print_Location__c = 'Printer China';
                //standard TL schedule
                pj.Units_Spec_Due_Date__c = a.pub_Publish_Date__c - 140;
                pj.Files_Due_At_Printer__c = a.pub_Publish_Date__c - 126;
                pj.Estimated_Ship__c = a.pub_Publish_Date__c - 84;
                pj.BPI_Forecast_Due__c = pj.Units_Spec_Due_Date__c - 7;
                
                pj.Standard_CPU__c = false;
                pj.RFQ_Required__c = true;
                NewJobs.add(pj);
            }else if(a.Color_BW_Interior__c != '1/1' && a.Color_BW_Interior__c != '' && a.Name == 'Paperback' && a.Title__r.Internal_imprint__c == 'Two Lions'){ //TL Paperback
                //standard location
                pj.Print_Location__c = 'Printer China';
                //standard TL schedule
                pj.Units_Spec_Due_Date__c = a.pub_Publish_Date__c - 140;
                pj.Files_Due_At_Printer__c = a.pub_Publish_Date__c - 126;
                pj.Estimated_Ship__c = a.pub_Publish_Date__c - 84;
                pj.BPI_Forecast_Due__c = pj.Units_Spec_Due_Date__c - 7;
                
                pj.Standard_CPU__c = false;
                pj.RFQ_Required__c = true;
                NewJobs.add(pj);
            }else if(a.Color_BW_Interior__c != '1/1' && a.Color_BW_Interior__c != '' && a.Title__r.Internal_imprint__c == 'Jet City Comics'){
                //standard location
                pj.Print_Location__c = 'Printer US';
                
                pj.Standard_CPU__c = false;
                pj.RFQ_Required__c = true;
                pj.Print_Job_Status__c = 'Attention Required';
                pj.Schedule_Exception__c = true;
                
                pj.notes__c = 'Jet City 4-Color Project. Confirm with JCC tracker for initial schedule.';
                
                Newjobs.add(pj);
                
            }else{
                pj.Print_Job_Status__c = 'Attention Required';
                pj.Schedule_Exception__c = true;
                pj.notes__c = 'Error regarding masterID Spec. Consult with PXM / AE to confirm spec and schedule.';
                Newjobs.add(pj);
            }
            
        }
        
        
    }
    if (!NewJobs.isEmpty()){
        insert(NewJobs);
    }
    
}

 
Best Answer chosen by Molson94
Molson94Molson94
Nevermind, figured it out and added in a Null Check above that section.