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
chikkuchikku 

Error Occurred: An Apex error occurred: System.NullPointerException: Attempt to de-reference a null object in apex

When I do the process it throws an error on it like this and unable to show merge fields too in pdf.

Error Occurred: An Apex error occurred: System.NullPointerException: Attempt to de-reference a null object

User-added image

 
public class sendAnEmail
{
     
      @InvocableMethod( label='Test' description='sends an email')
       
    public static void sendEmailWithAttachment(List<id> listofQuoteHeader)
    {   
      
        // Application__c applink = [SELECT Id,Name FROM Application__c  ];
        Map<Id, Application__c> quotesMap =  new Map<Id, Application__c>([SELECT id,Contact__r.Email,Contact__r.Name FROM Application__c WHERE Id IN :listofQuoteHeader]);
           for(Id QuoteHeaderid :listofQuoteHeader)
           {
               PageReference pref= page.PDFGEN;
               pref.getParameters().put('id',(Id)QuoteHeaderid);
               pref.setRedirect(true);
                system.debug('--appID-'+QuoteHeaderid);
               Attachment attachment = new Attachment();      
               Blob b=pref.getContentAsPDF();
               attachment.Body = b;
               attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Quote' + '.pdf';
               attachment.IsPrivate = false;
               attachment.ParentId = QuoteHeaderid;
               attachment.Name='Sign.png';
               insert attachment;
               
               Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
               Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
               attach.setFileName('AttachmentEmailFile.pdf');
               attach.setBody(b);
               semail.setSubject('Quote Issued');
            //  String[] emailIds= new String[]{'abc@gmail.com'}; 
                String[] emailIds= new String[]{quotesMap.get(QuoteHeaderid)?.Contact__r.Email};
               semail.setToAddresses(emailIds);
               semail.setPlainTextBody('Please find the attached quote details');
 semail.setHtmlBody('Hello ' +  + '<br/><br/>Summary report' + contact.Name+'.<br/><br/><a href="'+ URL.getSalesforceBaseUrl().toExternalForm() + '/' + QuoteHeaderid+'">Click Here</a> Please check your Summary report');

               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
                Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
                  system.debug('----'+listofQuoteHeader);
           } 
        
 SendAnEmail.sendAnEmail();// I have call the constructor method in invocable method
    }
    // this method for merge fields
       private static  Application__c app{get; private set;}
       public static void sendAnEmail() {
          
               Id id = ApexPages.currentPage().getParameters().get('id');
                app = (id == null) ? new Application__c() : 
            [SELECT id,Name FROM Application__c WHERE Id = :id];
           }
           
}
<apex:page  controller="sendAnEmail" renderAs="advanced_pdf" applyHtmlTag="false" applyBodyTag="false" standardStylesheets="false"
showHeader="false" sidebar="false">>
   <apex:form >
             You belong to app Name:{!app.Name}
          </apex:form> 
    
     <h1>
    
      hello
    </h1>
  
</apex:page>
please help me out

 
ShivankurShivankur (Salesforce Developers) 
Hi Chikku,

The error mentioned in screenshot is pointing out to a process named 'Test' in your org.

So there must be some field which is getting set as null value and so is the error.

Please try to add more conditions in your process builder to check and ensure if all the fields have values for assignation in your apex getting invoked.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.