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
persistent 129persistent 129 

System.VisualforceException: Attempt to de-reference a null object in Page.getContent

Hi,
My Code bolow,
PageReference  pr = Page.VF_Page_Here;
String mailBody = pr.getContent().toString();

The above code was working fine abount a few day's ago. All of a sudden it has started throwing exception,
System.VisualforceException: Attempt to de-reference a null object

When I run the same code through workbench it works fine, but when called from an apex class throws the above excpetion.

Has somebody faced similar issue & got any resolution over it?
Is this some versioning problem?

Please help!!
Thanks in Advance!
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your code for VF_Page_Here and controller
persistent 129persistent 129

VF_Page_Here.page

<apex:page showHeader="false" sidebar="false" controller="SampleController">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
	<table width="780" border="0" cellpadding="0" cellspacing="0" style="border:1px #949494 solid; font-family:Arial, Helvetica, sans-serif; color:#000; font-size:15px" align="center">
	   
		<tr>
			<td colspan="5" style="padding:0 0 0 10px;">
				<strong>Application Reference Number:</strong> {!custRefNo}
			</td>
		</tr>
		<tr>
			<td colspan="5" style="padding:10px 0 0 10px;">
				Dear {!name},
			</td>
		</tr>
		<tr>
			<td colspan="5" style="padding:10px 0 0 10px;">
			   Thank you for completing the Personal Details section in the online application form.<br /><br />
				Your Application Reference Number is {!custRefNo}<br /><br />
			</td>
		</tr>    

		<tr>
			<td colspan="5" valign="top" style="padding:10px 0 0 10px;font-size:13px;">
				This is a system generated alert and does not require a signature. We request you not to reply to this message. This e-mail<br />is confidential and may also be privileged. Please do not copy or use it for any purpose, nor disclose its contents to any<br /></a>
			</td>
		</tr>
		   
		 


		<tr>
			<td colspan="5" style="padding:10px 10px 0px 10px; font-size:12px; line-height:16px" >Terms and conditions apply</td>
		</tr>

		<tr>
			<td valign="top"><img src="images/spacer.gif" width="379" height="1" alt=""/></td>
			<td valign="top"><img src="images/spacer.gif" width="274" height="1" alt=""/></td>
			<td valign="top"><img src="images/spacer.gif" width="42" height="1" alt=""/></td>
			<td valign="top"><img src="images/spacer.gif" width="39" height="1" alt=""/></td>
			<td valign="top"><img src="images/spacer.gif" width="46" height="1" alt=""/></td>
		</tr>
	  </table>
</body>
</apex:page>

SampleController.cls
public class SampleController {

    public Salaried__c salObj {get;set;}
    public String salId {get;set;}
    public String name {get;set;} 
    public String custRefNo {get;set;}
    
    public SampleController(){
        salId = ApexPages.currentPage().getParameters().get('salId');
        custRefNo = ApexPages.currentPage().getParameters().get('custRefNo');
        salObj = new Salaried__c();
        
        if(salId != null){  
            List<Salaried__c> salList = new List<Salaried__c>();
            salList = [Select id,name,First_Name__c,Middle_name__c,Last_Name__c FROM  Salaried__c WHERE id=: salId limit 1];
			if(salList != null && salList.size() > 0){ 
				salObj = salList[0];
				name = salObj.First_Name__c +' '+ salObj.Middle_name__c+' '+salObj.Last_Name__c;
			}       
        }
    }
}


When I directly run this page, by passing query parameters, it is woking fine. But when accessed as below throws the exception,
 
PageReference  pr = Page.VF_Page_Here;
pr.getParameters().put('salId',sal.id);
pr.getParameters().put('custRefNo',sal.Cust_Reference_No__c);
String mailBody = pr.getContent().toString();
Amit Chaudhary 8Amit Chaudhary 8
Make sure while creating sal record you are passing value for below field

salObj.First_Name__c
salObj.Middle_name__c
salObj.Last_Name__c
Let us know if this will help you
persistent 129persistent 129

Yes, the fields are not empty.
When I run the same code from workbench it does work wihout any error, but when called from another Controller method facing the error!

Amit Chaudhary 8Amit Chaudhary 8
Please post both code which you are executing from workbench and from controller