3

I am trying to add a Visual Force component into a VisualForce email template but keep getting the error message below:

[Error] Error occurred trying to load the template for preview: Attempt to de-reference a null object. Please try editing your markup to correct the problem.

I do not get any errors when saving class or component.

Apex Class:

public class InteractionsForCustomer {


private List<Interactions__c> allInteractions;
    private Interactions__c currentInteractions;

public InteractionsForCustomer() {

 }

public List<Interactions__c> getAllInteractions() {
            allInteractions = [Select Id, Name from Interactions__c where Stage__c = 'Open' AND Cutomer__c = :currentInteractions.Customer__c];
    return allInteractions;
}

 public Interactions__c getcurrentInteractions() {       
         return currentInteractions;
 }
 public void setcurrentInteractions(Interactions__c val) {
        this.currentInteractions = val;     
  }

}

VisualForce Component:

<apex:component controller="InteractionsForCustomer" access="global">
    <apex:dataTable value="{!allInteractions}" var="inv">
      <apex:column >
        <apex:facet name="header">Invoice Name</apex:facet>
           {!inv.Customer__c}
       </apex:column>
     </apex:dataTable>
 </apex:component>

Can anybody help??

Thanks Josh

3
  • Where you are setting up currentInteractions ? you are missing <apex:attribute for component.
    – Himanshu
    Commented Oct 19, 2015 at 14:44
  • Hi @Himanshu, I am very new to apex can you please confirm where I am missing this Commented Oct 19, 2015 at 14:46
  • can you update your question with your email template as well ?
    – Himanshu
    Commented Oct 19, 2015 at 14:57

1 Answer 1

4

The general reason for this error is that the VF email template previewer doesn't have enough information to pass to the component's controller all such information as is typically provided at runtime when you either test the template (by providing a valid ID of the record used in relatedTo attribute) or use in APEX or a workflow/process builder.

To solve this and avoid the error message, you need to code the component controller to handle null values for all inputs in a graceful way (that is, assume all inputs can be null - check for null, and handle accordingly without throwing exceptions)

For example - the most likely cause of the NPE is that currentInteractions is null because the setter for it was never invoked. Thus rewrite this method to:

public List<Interactions__c> getAllInteractions() {
        allInteractions = currentInteractions != null
         ? [Select Id, Name from Interactions__c where Stage__c = 'Open' AND Cutomer__c = :currentInteractions.Customer__c]
         : new List<Interactions__c>();
  return allInteractions;
}

That said, @Himanshu asks a good question - how does currentInteractions get set in the component controller; normally you would pass in an attribute from the containing VF page that would in turn be sent to the component controller via an apex:attribute assignto={!currentInteractions} attribute.

N.B. It is unusual for an SOBject to be named implying a plural , e.g. "Interactions__c" as opposed to "Interaction__c". Plurals are used for variables that represent lists or sets. Think of Account, Opportunity, Quote, etc.

5
  • Hi @crop1645 is it possible you could provide some example code? I am fairly new to both apex and visualforce so still trying to figure certain things out. Any help is appreciated Commented Oct 20, 2015 at 8:52
  • i made some edits
    – cropredy
    Commented Oct 20, 2015 at 18:19
  • Hi @crop1645 maybe easier if I explain my requirement. I need to create a list of all the invoices on a customer when we email out the invoice. So the Child Object is Invoice and Parent is Customer. Therefore when we send out email to customer at invoice level we need to show all other invoices that customer has open in a table in the VF email template. Commented Oct 21, 2015 at 8:02
  • Josh -- correct if I'm wrong but the issue with de-referencing a null object is addressed by the answers herein; The above comment looks like a different question and should be posted as such if you are still having difficulties figuring out how to traverse the schema
    – cropredy
    Commented Oct 21, 2015 at 16:01
  • Yes this got rid of the error message however the table did not have any records showing just the Header Invoice Name? Commented Oct 22, 2015 at 7:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .