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 with VisualForce Page

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()")

 

The code from the positionExtension Apex Class:

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) 
dragonmagicldragonmagicl

It might help if you actually assign something to the value of theController variable say in the constructor like

 

theController = positionController;

 

;)

All Answers

dragonmagicldragonmagicl

It might help if you actually assign something to the value of theController variable say in the constructor like

 

theController = positionController;

 

;)

This was selected as the best answer
SCRSCR

Thanks for the quick response, but I'm still a bit confused about where in the positionController Apex Class code that I assign the value per your recommendation.  Would you please be a bit more specific?

 

Thanks,

SCCR

SCRSCR

Hello - I really apprecaite your advice, but I do not understand where exactly to assign theController = positionController; code.  I'm sure this is a common mistake that newbies make.  I am going through the Force.com Developers Guide doing the project and exercises.  This code (with the problem) was included in the Code Share for this project.  I have tried to make an assignment, but I just can't figure out the proper method.  Would you please specifically point out in my code where I would make the change you are suggeesting?

 

Thanks,

SCR

SCRSCR

Thanks for the tip... problem solved.

SCR

HareHare

Dynamic dependent pick list using apex :  i created one Location__c object    in that i Created two Pick Lists Country__c  and  State__c

 

in Country__c pick list i added  India,pakistan valus and in state__c pick list  i added Andrapradesh,Madhyapradesh,lohore,Quetta

 

if  i select a value as India from Country__c pick list in State__c only able to see Andrapradesh,Madhyapradesh

if i select a value as pakistan from country_c picklist in state__c only able to see lohore,Quetta   

 

by using apex