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
manjunath vivekmanjunath vivek 

Unable to sum the field values using controller

Hi all,

My visualforce code and controller is as follows.

<apex:page standardcontroller="Bankloan__c" extensions="inform">
<apex:form >
  <apex:pageBlock >
   <apex:pageblockTable value="{!bank}" var="b">
    <apex:column value="{!b.EMI__c}"/>
    <apex:column value="{!b.Bank_name__c}"/>
    <apex:column value="{!b.Loan_type__c}"/>
    <apex:column value="{!b.EMI_date__c}"/>
    <apex:column value="{!b.Loan_taken__c}"/>
    
   </apex:pageblockTable>
   
   <apex:outputText value="{!emi}"/>
   <BR></Br>
    <apex:outputText value="{!loanamount}"/>
  </apex:pageBlock>
  </apex:form>
  
</apex:page>




Public  class inform{

Public List<Bankloan__C> Bank {get;set;}


    public inform(ApexPages.StandardController controller) {
Bank=[Select EMI__c,Bank_name__c,Loan_type__c,EMI_date__c,Loan_taken__c from Bankloan__c];
    }
  
  
Public Decimal getemi(){  
 
  Decimal sum=0;


  For(Bankloan__C BK: Bank){
    
Sum+=BK.EMI__C;

}
Return Sum;

}


Public Decimal getloanamount(){  
  Decimal summ=0;
    For(Bankloan__C BK: Bank){

Summ=BK.Loan_taken__c;

  }
Return Summ;

}

}


It works fine, but when I change Summ=BK.Loan_taken__c;( I have written it in bold letters in the above code)
 to Summ+=BK.Loan_taken__c;, it throws an error message "System.NullPointerException: Argument cannot be null. 
Class.inform.getloanamount: line 19, column 1",I a not able to fix the issue, i have used same type of method in the above code
for retrieving EMI,without any issue.I made sure there are values available for Loan_taken__c field, still unable to fix he isssue.

Can some one help me please?
BDatlaBDatla
Hi Vivek,

Please use the below code to check null values :

if( BK.Loan_taken__c != null)
     Summ+=BK.Loan_taken__c;
Let me know for any issues

Regards,
BDatla
Sagar PareekSagar Pareek
It should be 
 
if( BK.Loan_taken__c != null)
     Summ+=BK.Loan_taken__c;

 
manjunath vivekmanjunath vivek
Thanks Datla and pareek, it worked fine, can you please let me know why we have to use that condition because in the above code i have not used if condition for getemi(), method, it worked fine, it did'nt work with loan amount();why if condition is mandatory for loanamount method?
BDatlaBDatla
That might be because your test data always having value for BK.EMI__C.
Let me know if you need any clarification.

Regards,
BDatla