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
Mooda MayuMooda Mayu 

Controller and Extension Related...

Hello Team,

I am new to salesforce, while practcing Calculator VFP, I have encountered the the below issue.

I have controll named; Calci_ctrl with the below apex class;

public class Calci_ctrl {
    
    public integer a {get; set;}
    public integer b {get; set;}
    public integer c {get; set;}
    
    public Calci_ctrl (){
        
        a = 0;
        b = 0;
        
    }
    
    public void multiply(){
        
        c = a*b;
  
    }
    
    public void devide (){
        
        c = a/b;
    }
}

And extension is;

public class Calci_extension {
    
    public integer a;
    public integer b;
    public integer c;
    
    public Calci_extension (Calci_ctrl Ctrrl){
       
    }
    
    public void addition (){
        
        c= a+b;
    }
    
        public void substract (){
        
        c= a-b;
    }

}

And VFP is;

<apex:page controller="Calci_ctrl" extensions="Calci_extension">
    <apex:form >
        <apex:pageBlock title="Calculator">
            <apex:pageBlockSection >
                <apex:inputText label="Enter 1st value" value="{!a}"/>
                <apex:inputText label="Enter 2nd value" value="{!b}"/>
                <apex:outputText label="Result" value="{!c}" id="a1"/>      
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="+" action="{!addition}"  rerender="a1"/>
                <apex:commandButton value="-" action="{!substract}" rerender="a1"/>
                <apex:commandButton value="x" action="{!multiply}" rerender="a1"/>
                <apex:commandButton value="/" action="{!devide}" rerender="a1"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

****
This above code working fine for multiplication and devide, however, getting error for addition and substract. Can someone please help me with the basics and code.

Here are the errors;
Attempt to de-reference a null object
Error is in expression '{!addition}' in component <apex:commandButton> in page calcipage: Class.Calci_extension.addition: line 14, column 1

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

Controll is for multiplication and Device
Extension is for addition and substraction.

Can someone help please?
 
{tushar-sharma}{tushar-sharma}
Because in second extension you forgot to initialise variables, Initialise them into second extensions as well.

 
public class Calci_extension {
    
    public integer a{get;set;}
    public integer b{get;set;}
    public integer c{get;set;}
    
    public Calci_extension (Calci_ctrl Ctrrl){
       a =b =c =0;
    }

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/