I'm very new to coding and trying to create a visualforce page form that will be published to external entities to get more leads, after keying in information required, hit save and the page will redirect to our homepage which is outside of SF. I have the code as below when ever I submit the form there is error happen:

Attempt to de-reference a null object

Error is in expression '{!savelead}' in component in page driver_capture: Class.redirectPg.savelead: line 17, column 1

VF Page:

<apex:page showHeader="true" sidebar="false" standardController="Lead" extensions="redirectPg" title="Register Form">

<apex:form id="frm" enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Thông tin" id="pg_blk">
        <apex:pageBlockSection showHeader="false" columns="2" id="block1">
            <apex:pageBlockSectionItem ><apex:outputLabel >Last Name: </apex:outputLabel></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:inputField id="lastName" value="{!lead.LastName}" required="true"/></apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem ><apex:outputLabel >First Name: </apex:outputLabel></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:inputField id="firstName" value="{!lead.FirstName}" required="true"/></apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem ><apex:outputLabel >Phone*: </apex:outputLabel></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:inputField id="MobilePhone" value="{!lead.MobilePhone}" required="true"/></apex:pageBlockSectionItem>

            <apex:inputHidden id="record_type" value="{!lead.RecordTypeId}"/>
        var inputRecordTypeEle = document.getElementById("{!$Component.frm.pg_blk.block1.record_type}");
            if (inputRecordTypeEle != null) {

            <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                        <apex:commandButton action="{!savelead}" value="Signup Now!"/>



public class redirectPg {
Public redirectPg(ApexPages.StandardController controller){


Lead lead;

Public PageReference savelead(){
    insert lead;
    PageReference reRend = new PageReference('https://abc.xyz/');
    return reRend;

Please help.

1 Answer 1


You need to assign the Lead record from the ApexPages.StandardController parameter/ to the variable:

Public redirectPg(ApexPages.StandardController controller){
    lead = (Lead)controller.getRecord();

Otherwise, this variable is null, thus causing the NullPointerException you're getting.

  • Thanks very much, it works with abit modification as below Public redirectPg(ApexPages.StandardController controller){ lead = (Lead)controller.getRecord(); }
    – Tuyến
    Nov 6, 2019 at 16:48
  • @Tuyến Good point. I'd forgotten a cast is necessary. I'll update the answer to reflect that.
    – sfdcfox
    Nov 6, 2019 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.