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
SCRSCR 

Standard Controller Extension Problem

Hello - I am getting the following error when attempting to Save a record using a VisualForce Page (VisualForcExtension) and a Controller Extension (positionExtension):

 

System.NullPointerException: Attempt to de-reference a null object

                                                                                                                                                   

Class.positionExtension.Save: line 54, column 16 External entry point (line 54 column 16 is underlined in the following code fragment "return theController.save()";)

 

public class positionExtension { public string positionTypeID {get; set;} public positionExtension(ApexPages.StandardController positionController) { this.position = (Position__c)positionController.getRecord(); } public List<selectOption> PositionTypeOptions {get { List<selectOption> positionTypes = new List<selectOption>(); for (Position_Type__c ptr : [select name from Position_Type__c pt where pt.Department__c = :position.Department__c order by Name ]) positionTypes.add(new selectOption(ptr.id, ptr.name)); if (position.Department__c != null) { positionTypes.add(new selectOption('other', 'Other')); } else { positionTypes.add(new selectOption('', 'Please select a department', true)); } return positionTypes; } private set;} public Position_Type__c newPositionType{ get{ if (newPositionType == null) { newPositionType = new Position_Type__c();} return newPositionType; } private set; } public void resetPositionType() { positionTypeID = null; } public PageReference Save() { if (positionTypeID == 'other') { try{ newPositionType.Department__c = position.Department__c; insert newPositionType; position.Position_Type__c = newPositionType.ID; } catch (DmlException e) { ApexPages.addMessages(e); } } else { position.Position_Type__c = positionTypeID; } return theController.save(); } private final Position__c position; private final ApexPages.StandardController theController; }

 

The markup from the Visualforce Page that calls the Apex Class:

 

<apex:page standardController="Position__c" extensions="positionExtension" > <apex:form > <apex:sectionHeader title="Add New Position"/> <apex:pageBlock mode="edit" id="thePageBlock"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlocksection title="Information"> <apex:inputField value="{!Position__c.Location__c}"/> <apex:inputField value="{!Position__c.Hiring_Manager__c}"/> <apex:inputField value="{!Position__c.Status__c}"/> <apex:inputField value="{!Position__c.Notification__c}"/> <apex:inputField value="{!Position__c.Start_Date__c}"/> </apex:pageBlocksection> <apex:actionRegion > <apex:pageblocksection columns="1" title="Department"> <apex:inputField value="{!Position__c.Department__c}"> <apex:actionSupport event="onchange" rerender="dependentPositionType" action="{!resetPositionType}" status="departmentStatus"/> <apex:actionStatus id="departmentStatus" startText="Fetching position types..."/> </apex:inputField> </apex:pageblockSection> </apex:actionRegion> <apex:pageblockSection id="dependentPositionType" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Position Type" for="pt"/> <apex:panelGrid columns="2"> <apex:actionRegion > <apex:outputText value="{!Position__c.Position_Type__c}" rendered="false"/> <apex:selectList id="pt" value="{!positionTypeID}" size="1" disabled="{!ISNULL(Position__c.Department__c)}"> <apex:selectOptions value="{!PositionTypeOptions}"/> <apex:actionSupport event="onchange" rerender="dependentPositionType" status="typeStatus"/> </apex:selectList> </apex:actionRegion> <apex:actionStatus id="typeStatus" startText="updating form..."> <apex:facet name="stop"> <apex:inputField value="{!newPositionType.Name}" rendered="{!positionTypeId == 'other'}" required="true"/> </apex:facet> </apex:actionStatus> </apex:panelGrid> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection title="Position Details"> <apex:inputField value="{!Position__c.Job_Description__c}"/> <apex:inputField value="{!Position__c.Responsibilities__c}"/> <apex:inputField value="{!Position__c.Programming_Languages__c}"/> <apex:inputField value="{!Position__c.Educational_Requirements__c}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

I would appreciate any ideas for resolving this problem.  Please go easy on newbie learning VF and Apex.

 

Thanks,

SCR

Best Answer chosen by Admin (Salesforce Developers) 
ShamSham

Hi SCR,

 

You have not initialized theController variable. you should initialize it in the constructor.

 

this.controller = positionController;

 

All Answers

jeffdonthemicjeffdonthemic

Sorry, but I do not see where line 54 is at? I don't see anything underlined?

 

Jeff Douglas

Appirio

http://blog.jeffdouglas.com 

ShamSham

Hi SCR,

 

You have not initialized theController variable. you should initialize it in the constructor.

 

this.controller = positionController;

 

This was selected as the best answer
SCRSCR
Thanks very much for your responses, all.  I was able to resolve the issue by initializing the theController variable to be positionController in the constructor as you've indicated.  I obtained the original code (with this flaw) from the Force.com Developer Guide and the force.com migration package files that accompany the custom Recruiting App project.  Perhaps it was done intentionally to impose some hard learned lessons upon the newbies.  It's all goodness. Cheers,Sean