+ Start a Discussion
uptime_andrewuptime_andrew 

Cannot Save - "You cannot call save() on a null object"

Hello,

 

I have a visualforce page which is giving me the error "You cannot call save() on a null object" on both new & and existing records when attempting to save.

 

I cannot see why it is giving this problem, as I'm using the standardController for saving.

 

Visualforce page:

 

 

<apex:page standardController="SalesOrder__c" extensions="MySalesOrderController"> <apex:form id="theForm"> <apex:pageBlock title="Sales Order"> <apex:pageBlockSection title="Information" columns="1"> <apex:inputField value="{!salesorder.Name}"/> <apex:inputField value="{!salesorder.Account__c}"/> <apex:inputField value="{!salesorder.Amount__c}"/> <apex:inputField value="{!salesorder.Assets_Total__c}"/> <apex:inputField value="{!salesorder.Invoice_Type__c}"/> <apex:inputField value="{!salesorder.Description__c}"/> <apex:inputField value="{!salesorder.Currency__c}"/> <apex:inputField value="{!salesorder.CAD_Amount__c}"/> <apex:inputField value="{!salesorder.ConversionRate__c}"/> <apex:inputField value="{!salesorder.Invoice_Date__c}"/> <apex:inputField value="{!salesorder.Account_Exec__c}"/> <apex:inputField value="{!salesorder.Revenue_Recognition_Date__c}"/> <apex:inputField value="{!salesorder.Opportunity__c}" /> </apex:pageBlockSection> <apex:commandButton action="{!save}" value="Save" /> <apex:commandButton action="{!cancel}" value="Cancel" /> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

 

Extension:

 

 

public class MySalesOrderController { public SalesOrder__c salesorder { get; set; } public Account account; public User user; public MySalesOrderController(ApexPages.StandardController controller) { if ( ApexPages.currentPage().getParameters().get('id') != null) { salesorder = [select Id, Account_Exec__c, Account__c, Amount__c, CAD_Amount__c, ConversionRate__c, Currency__c, Description__c, Invoice_Date__c, Invoice_Type__c, Revenue_Recognition_Date__c, Name, Assets_Total__c, Opportunity__c from SalesOrder__c where id = :ApexPages.currentPage().getParameters().get('id')]; } else { salesorder = new SalesOrder__c(); salesorder.Account__c = ApexPages.currentPage().getParameters().get('AccountId'); } user = getUser(); if(user != null) { salesorder.Account_Exec__c = user.Id; } } public String getName() { return 'MySalesOrderController'; } public Account getAccount() { if(salesorder.Account__c != null) { account = [select id, name, ownerid from Account where id = :salesorder.Account__c]; } else { System.debug('no Account Id '); account = new Account(); } return account; } public User getUser() { if (ApexPages.currentPage().getParameters().get('id') != null) { if(salesorder.Account_Exec__c != null) { user = [ select id, name from user where id = :salesorder.Account_Exec__c]; } } if(user == null) { account = getAccount(); if(account.OwnerId != null) { user = [ select id, name from user where id = :account.OwnerId]; } } return user; } }

 

 

 

 

dhcrusoedhcrusoe

Hey there,

 

We had the same issue... not sure why it happened, but upsert fixed it - here's our final codeblock:

 

 

try{ if (pageRef.getParameters().get('lessonNumber') != null) { lessonplan.Lesson_Number__c = pageRef.getParameters().get('lessonNumber'); } lessonplan.Name = 'Web Submission'; lessonplan.Apprenticeship__c = pageRef.getParameters().get('apprenticeshipId'); lessonplan.Last_CT_Modified__c = pageRef.getParameters().get('ctContactId'); upsert(lessonplan); }catch(Exception e){ String myErr = '?err=' + e.getMessage(); PageReference ref = new PageReference('/apex/testerr' + myErr); ref.setRedirect(true); return ref; }

 

 

 

r2r2

You are referencing your extension for everything except the save and cancel methods. You have no references to your standardcontroller. So when you use your standard {!save} method it has a NULL object. You should add a custom save method to your extension and use the extenion a controller instead of using the standard controller. 

 

OR

 

You can start again and use the standard controller only and create another extension to prepopulate fields you need pre populated.

 

Good luck =)