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
Best Answer
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: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:attributeassignto={!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.