+ Start a Discussion
EvannnEvannn 

Cannot call edit() on null object error - Controller extension

Hello,

 

I am a newbie Apex Developer trying to solve an issue with calling the StandardController.edit() method.

 

I created a custom Organization__c object for a user to enter info about their company.   I want logic in the class to limit t 1 instance of Organization__c, so I over-rode the "New" button on the object to point to a Visualforce page that calls my determinePageRedirect method.  If the list of Organization__c is > 0, it returns a PageReference to the edit page using the current ID.  If the list is null, I want to call StandardController.edit() to show the regular new page for Organization__c.  

 

Code works as expected when this.existingOrganization.size () > 0. however, I get an error message "cannot call edit() on null object when that list is empty. Seems like I need some mojo on my StandardController instantiation to make this work. Any ideas? Any help is greatly appreciated - this is driving me nuts!

 

Thanks

 

Apex Class

 

public class OrganizationValidation {

	//declare a private controller variable
	private ApexPages.StandardController controller;
	
	private Organization__c org {get;set;}
	
	//populate list of existingOrganizations
	public List<Organization__c> existingOrganizations;
		public List<Organization__c> getExistingOrganizations(){
			return existingOrganizations;	
	}

	//instatiate class
	public OrganizationValidation(){	
	}
	
	//standard controller constructor
	public OrganizationValidation(ApexPages.StandardController pController) {
        this.controller = pController;
        System.debug('***this.controller: ' +this.controller);
		
		//need StandardController to be aware of current context
		
		
		this.org = (Organization__c)pController.getRecord();
		System.debug('***org: '+ org);

    	OrganizationRedirect();
    }
    
	//Run these methods when we hit the Visualforce page
	public PageReference OrganizationRedirect(){
		
			
		//invoke method to query if there is an Organization__c result
		queryOrganizations();
		
		//invoke method to redirect based on if/then
		determinePageRedirect();
		
		return null;
	
	}
	
	//PageReference method – button on Visualforce page calls
	public PageReference editButtonRedirect() {

		PageReference editRedirect;
		for(Organization__c org: this.existingOrganizations){
				Organization__c myOrg = org;
				editRedirect = new PageReference ('/' + myOrg.id + '/e');
		
		}
	
		return editRedirect;
	
	}
		
	public void queryOrganizations(){
		//query the orgID
		this.existingOrganizations = [Select id, Name From Organization__c];
		//System debug
		System.debug('***existingOrganizations: '+existingOrganizations);
		
	}
	
	//Method to run if/else statement to redirect page
	public PageReference determinePageRedirect(){
	
		PageReference redirectPage;
		if (this.existingOrganizations.size() > 0){
			
			redirectPage = new PageReference ('apex/OrganizationRedirect');
			
		}else{
			
			//Show new page
			redirectPage = this.controller.edit();
			
		}
	
return redirectPage;
	
	}
}

 

Visualforce Page

 

<apex:page standardController="Organization__c" extensions="OrganizationValidation" action="{!determinePageRedirect}">
	<apex:outputPanel >
		<apex:form >
			<h1>Warning</h1>
			<p>You already entered your Organization's information.  Please edit the existing record.</p>
			<apex:commandButton action="{!editButtonRedirect}" value="Edit Organization" />
		</apex:form>
	</apex:outputPanel>
</apex:page>

 

 

 

 

 

jeffdonthemic2jeffdonthemic2

I think the problem is the way you are initializing the class. At a quick glance this resembles more of a custom controller than a controller extension. You initialized a extension like:

 

 

public myControllerExtension(ApexPages.StandardController stdController) {
    this.acct = (Account)stdController.getRecord();
}

 

 

You might want to take a look at the Visualforce docs for creating an extension.

 

HTH

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook

EvannnEvannn

Hi Jeff,

 

Thanks for the link.  I'm taking another crack at it tonight and will let you know how it goes.

 

 

Cheers,

Evan

JeeedeeeJeeedeee

Did you get it fixed, now having the same problem with below code... What I am doing is just showing some info, and then want to go to the orininal edit page.

public NewAccountController(ApexPages.StandardController controller) { 
	this.controller = controller;
	this.account = (Account) controller.getRecord();
}

// When I call this from a button in my VF page, it goes wrong. // The debug info shows that the account is filled
public PageReference createNewTP() {
  	System.debug('Entering create new tp, the value of account is' +account);
	PageReference newPage = controller.edit();
		
	return newPage;		
}

 

 

Debug info, shows that the account is not null

 

Entering create new tp, the value of account isAccount:{Name=Test, BillingPostalCode=3436}

 

Anybody has any clue what I am doing wrong?

NMackeyNMackey

I don't think StandardController.edit() works unless the sObject associated with it has a valid ID. How can you return a page reference to edit a record that doesn't exist?

 

That said, I wish it was different, too.