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
AVINASH UPADHYAAVINASH UPADHYA 

Simple Calculator!


Hi All,

I am trying to build a Simpale calculator in different way. I have created a custome object Calculator__C having 3 fields Var1, Var2, Result.

Bellow is the controller for same but its not working. Please suggest me what needs be modified in bellow code.

public class CalculatorController {
    public Calculator__C  Calc  {get; set;}
   
Public void Add(){
      Calc.Result__c = Calc.Var1__c + Calc.Var2__c; 
        }
Public void Sub(){
      Calc.Result__c = Calc.Var1__c - Calc.Var2__c; 
    }  
Public void Div(){
     Calc.Result__c = Calc.Var1__c / Calc.Var2__c; 
    }
   
}
Best Answer chosen by AVINASH UPADHYA
pconpcon
My guess is that you are getting this because you never initialized your Calc variable.  You should add constructor to do this for you.
 
public class CalculatorController {
    public Calculator__c calc {
        get;
        set;
    }

    public CalculatorController() {
        this.calc = new Calculator__c();
    }

    //....
}

You will also need to do a re-render [1] of your result if you want to see that it changed.

[1] https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionRegion.htm

All Answers

pconpcon
Can you please also include your VisualForce page that is using this controller?  And expand on what you mean by "not working?"

NOTE: Please use the "Add a code sample" button (icon <>) to help increase readability
AVINASH UPADHYAAVINASH UPADHYA
<apex:page controller="CalculatorController">
    <apex:form >
     <apex:pageBlock >
      
    <apex:inputField value="{!Calc.Var1__c}" />
        <apex:inputField value="{!Calc.Var2__c}" />
        <apex:outputField value="{!Calc.Result__c}" />
             
         </apex:pageBlock> 
             
        <apex:pageBlock > 
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Add}" Value="+"/>
            <apex:commandButton action="{!Sub}" Value="-"/>
            <apex:commandButton action="{!Div}" Value="/"/>
        </apex:pageBlockButtons>
        
        </apex:pageBlock>
	</apex:form>
</apex:page>

I am getting a error as below

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Add}' in component <apex:commandButton> in page vfcalculator: Class.CalculatorController.Add: line 11, column 1
Class.CalculatorController.Add: line 11, column 1
pconpcon
My guess is that you are getting this because you never initialized your Calc variable.  You should add constructor to do this for you.
 
public class CalculatorController {
    public Calculator__c calc {
        get;
        set;
    }

    public CalculatorController() {
        this.calc = new Calculator__c();
    }

    //....
}

You will also need to do a re-render [1] of your result if you want to see that it changed.

[1] https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionRegion.htm
This was selected as the best answer
Mithun S NaikMithun S Naik
Hai,
If you want to develop a Simple calculator using Aura then the below link will be helpful to you.
https://viewonreview.com/salesforce-lightning-tutorial-for-beginners/