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
Alex Kirby 8Alex Kirby 8 

Attempt to de-reference a null object Error is in expression

Hello Everyone,

First post on here, pleae be gentle. I have recently started working my way intp apex and visualforce etc and I have come across a problem.

I am trying to create an opportunity off the back of another i.e. for cross selling purposes. I want a custom button which opens a pop up where information from the opportunity is held and then used with new information to create a new opportunity.

I have managed to get the button and popup working and I can now see old opportunity data and some blank fields to enter data in, hoorah! Now I have a problem when it comes to saving.

The Error:

Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page iframecontent: Class.OpportunityInformationPage.save: line 19, column 1

The Class:

public class OpportunityInformationPage {

     private final Opportunity oppy;
  
    public OpportunityInformationPage (ApexPages.StandardController
        stdController) {
   this.oppy = (Opportunity)stdController.getrecord();

    }
Public opportunity oppstring{get; set;}
public OpportunityInformationPage(){
oppstring= new opportunity();
}

public void save(){

opportunity opp= new opportunity();

opp.Accountid= oppstring.accountid;
system.debug(oppstring.accountid);
opp.name= oppstring.name;

opp.closedate= oppstring.closedate;

opp.StageName= oppstring.StageName;

opp.leadSource= oppstring.LeadSource;

opp.Ownerid= oppstring.Ownerid;

opp.Amount= oppstring.Amount;

opp.Type= oppstring.Type;

opp.Probability= oppstring.Probability;

opp.Product_type__c = oppstring.Product_type__c;


insert opp;

}


}

The VF Page:

<apex:page showHeader="false" sidebar="False" standardcontroller="Opportunity" extensions="OpportunityInformationPage">

<apex:form >

<apex:pageBlock >

<apex:commandButton value="save" action="{!save}"/>

<apex:pageBlockSection columns="2" title="Old Opportunity Information">

<apex:outputfield value="{!opportunity.Product_Type__c}"/>
<apex:outputfield value="{!opportunity.closedate}"/>
<apex:outputfield value="{!opportunity.Start_Date__c}"/>
<apex:outputfield value="{!opportunity.End_Date__c}"/>

</apex:pageBlockSection>

<apex:pageBlockSection columns="2" title="Opportunity Information">


<apex:inputField value="{!opportunity.Ownerid}"/>

<apex:inputField value="{!oppstring.Amount}"/>

<apex:inputField value="{!oppstring.CloseDate}"/>

<apex:inputField value="{!oppstring.name}"/>

<apex:inputField value="{!oppstring.Accountid}"/>

<apex:inputField value="{!oppstring.StageName}"/>

<apex:inputField value="{!oppstring.Type}"/>

<apex:inputField value="{!oppstring.Probability}"/>

<apex:inputField value="{!oppstring.LeadSource}"/>

<apex:inputfield value="{!oppstring.Product_Type__c}"/>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:page>

Any help will be appreciated as I mentioned I am no hero when it comes to Apex.

Thanks in advance.

Alex
Best Answer chosen by Alex Kirby 8
Grazitti TeamGrazitti Team
Hi Alex,

The VF page has standardContoller = opportunity, so when this page loads, the standardController constructor will be called and not the default zero argument constructor. 

So modify your standard controller constructor :

public OpportunityInformationPage (ApexPages.StandardController
        stdController) {
   this.oppy = (Opportunity)stdController.getrecord();
 //Add this line, you must initialize and oppString object.
  oppstring= new opportunity();
    }
Hope this helps.


All Answers

AshlekhAshlekh
Hi,

When you click on butto nwhich calll save function it gives you error beccause your save funciton try to access oppstring.accountid. but Oppstring is not intilize before accessing the accountId.

There is a function OpportunityInformationPage() which will intilize the Oppstring but I don't think you call this function to intilize the Oppstring.


IF it helps you than please mark it as a solution and ENJOY APEX
Grazitti TeamGrazitti Team
Hi Alex,

The VF page has standardContoller = opportunity, so when this page loads, the standardController constructor will be called and not the default zero argument constructor. 

So modify your standard controller constructor :

public OpportunityInformationPage (ApexPages.StandardController
        stdController) {
   this.oppy = (Opportunity)stdController.getrecord();
 //Add this line, you must initialize and oppString object.
  oppstring= new opportunity();
    }
Hope this helps.


This was selected as the best answer
Alex Kirby 8Alex Kirby 8
Thanks @Grazitti Team that did the trick, @D-Horse, thank you for replying.