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
Abc234Abc234 

Attempt to de-reference a null object. on Visual force template

Hi All,

 

I am getting Attempt to de-reference a null object while opening visual force template.

 

Here is the code:

 


Template Code :
======================

<messaging:emailTemplate subject="New Template" recipientType="User" relatedToType="Custom__object__c">
    <messaging:plainTextEmailBody >
      <c:Test_EmailTemplatecomponent AccId="{!relatedTo.ID}"/>   
    </messaging:plainTextEmailBody>
</messaging:emailTemplate>


Component :
===============

<apex:component controller="Test_EmailTemplatecontroller" access="global">
     <apex:attribute name="AccId" description="My Account ID." type="String" required="required" assignTo="{!accountId}"/>
     <h1>Congratulations</h1>
     <p>{!accountName}</p>
</apex:component>

Controller:
=============

public without sharing class Test_EmailTemplatecontroller
{
     private String cAccountId;
     private Custom__object__c cAccount;

     public String getAccountId()
     {
System.debug('####################Account ID : ' + cAccountId);
          return cAccountId;
     }

     public void setAccountId(String id)
     {
System.debug('###################################### id: ' + id);
          cAccountId = id;
          initializeAccount();
     }

     public String getAccountName()
     {
System.debug('#####################################name: ' + cAccount.Name);
          return cAccount.Name;
     }

     private void initializeAccount()
     {
System.debug('################################initializeAccount');
          if(cAccountId != null) {
               Custom__object__c cAccount = [SELECT Name, Id FROM Custom__object__c WHERE ID = :cAccountId];
          }
     }
}

                 
>> I am not getting any error When I comment out getAccountName method but I want to dispay the account name here

 

 Can any one help me to how to resolve this?

 

Thanks,
                

 

Abc234Abc234

If I commented out code below and no issue

 

public String getAccountName()
     {
System.debug('####################################

#name: ' + cAccount.Name);
          return cAccount.Name;
     }

 

gbu.varungbu.varun

Hi,

 

You are getting error as you did not assign memory to Custom__object__c as  cAccount = new Custom__object__c();

akzeakze
Reason: Your cAccount object is not initialized. What you are initializing is a local variable within the InitializeAccount() method, but not initializing the class level cAccount object and when you access this in the page, you will get the null object error.