• Opportunities set to best few or better require at least one related custom object record for objects Top 5 product Codes
  • Created custom visual force page using opportunity standard controller and controller extension to allow users to create opportunity and up to 5 product code records simultaneously
  • Cannot figure out how to save. Tried a good ol' insert and get error:

    Attempt to de-reference a null object Error is in expression '{!saveAll}' in component in page oppcustomcreate: Class.topFiveProductCodes.saveAll: line 21, column 1

Relevant vf code:

<apex:pageBlockSection title="Top 5 Product Code Info" columns="3">

    <apex:inputField id="prodOne" label="Product Code" value="{!opportunity.product_code_list__c}" />
    <apex:inputField label="Price" value="{!prodOne.price__c}" />
    <apex:inputField label="Per" value="{!prodOne.per__c}" />

    <apex:inputField id="prodTwo" label="Product Code" value="{!opportunity.product_Code_list__c}" />
    <apex:inputField label="Price" value="{!prodTwo.price__c}" />
    <apex:inputField label="Per" value="{!prodTwo.per__c}" />

    <apex:inputField id="prodThree" label="Product Code" value="{!opportunity.product_Code_list__c}" />
    <apex:inputField label="Price" value="{!prodThree.price__c}" />
    <apex:inputField label="Per" value="{!prodThree.per__c}" />

    <apex:inputField id="prodFour" label="Product Code" value="{!opportunity.product_Code_list__c}" />
    <apex:inputField label="Price" value="{!prodFour.price__c}" />
    <apex:inputField label="Per" value="{!prodFour.per__c}" />

    <apex:inputField id="prodFive" label="Product Code" value="{!opportunity.product_Code_list__c}" />
    <apex:inputField label="Price" value="{!prodFive.price__c}" />
    <apex:inputField label="Per" value="{!prodFive.per__c}" />


Controller Extension

public class topFiveProductCodes {
    Public List < Top_5_Product_Codes__c > codeList = New List < Top_5_Product_Codes__c > ();
    public Opportunity opp {get;set;}
    public Top_5_Product_Codes__c prod = New Top_5_Product_Codes__c();
    public Top_5_Product_Codes__c prodOne {get;set;}
    public Top_5_Product_Codes__c prodTwo {get;set;}
    public Top_5_Product_Codes__c prodThree {get;set;}
    public Top_5_Product_Codes__c prodFour {get;set;}
    public Top_5_Product_Codes__c prodFive {get;set;}
    public ApexPages.StandardController myCodeController {get;set;}
    public topFiveProductCodes(ApexPages.StandardController oCon) {
        myCodeController = new ApexPages.StandardController(prod);
        this.opp = (Opportunity) oCon.getRecord();
    public ApexPages.PageReference saveAll() {
        insert opp;
        prodOne.Opportunity__c = opp.Id;
        prodTwo.Opportunity__c = opp.Id;
        prodThree.Opportunity__c = opp.Id;
        prodFour.Opportunity__c = opp.Id;
        prodFive.Opportunity__c = opp.Id;
        insert prodOne;
        insert prodTwo;
        insert prodThree;
        insert prodFour;
        insert prodFive;
        return null;

Line 21 is:

prodOne.Opportunity__c = opp.Id;

2 Answers 2


The statement insert opp; seems to be failing. If the opportunity in opp is not inserted and in the next line Id of opp is being accessed, then error should be occuring

Update A new object for prodOne and other prod variables should be created in the constructor. And also there should be null check before prodOne.Opportunity__c = opp.Id;

public topFiveProductCodes(ApexPages.StandardController oCon){
    prodOne = new Top_5_Product_Codes__c();
    myCodeController = new ApexPages.StandardController(prod);
  • That's what I thought too but each time I try to run it I'm able to verify the opp was inserted correctly
    – SFDC Noob
    Jun 9, 2016 at 22:18
  • is the value getting set in prodOne from vf page?
    – Cloud Man
    Jun 9, 2016 at 22:20
  • nevermind, shouldn't matter
    – Cloud Man
    Jun 9, 2016 at 22:21
  • system debug shows prodOne as null...
    – SFDC Noob
    Jun 9, 2016 at 22:23
  • ok.thats the reason then..another thing i noticed is an object should be created for prodOne. updated the answer
    – Cloud Man
    Jun 9, 2016 at 22:28

prodOne is not instantiated, it is just mentioned as getter/setter.

The way Opportunity opp is instantiated and assigned a value, can you able to do that. Hopefully it will solve your issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .