+ Start a Discussion
SFDC DummySFDC Dummy 

VF page:System.NullPointerException: Attempt to de-reference a null object

Hi

I have created a controller for searching record.there is no error in code it saved .but at the time of searching record it will dispaly error..

Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!displaingTable}' in component <apex:commandButton> in page newtrial1: Class.dateInPBTbaleControllernew1.calculateTotalRevenue: line 84, column 1
Class.dateInPBTbaleControllernew1.displaingTable: line 77, column 1
Class.dateInPBTbaleControllernew1.calculateTotalRevenue: line 84, column 1
Class.dateInPBTbaleControllernew1.displaingTable: line 77, column 1

 
public with sharing class dateInPBTbaleControllernew1 {
    public BankBook_Entry__c account{get;set;}
    public List<BankBook_Entry__c> listAccount {get;set;}
    public Double totalRevenue {get;set;}
    public Double totalRevenue1 {get;set;}
    public Double totalRevenuee {get;set;}
    public Double totalRevenuee1 {get;set;}
   
    public Date dat {get;set;}
    
    public Map<Date,List<BankBook_Entry__c>> mapOfDateWithEntry = new Map<Date,List<BankBook_Entry__c>>();
    
    public dateInPBTbaleControllernew1() {
    listAccount  = new List<BankBook_Entry__c>([SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                        FROM BankBook_Entry__c
                        order by Master_Code1__c ASC ]);
        
        List<BankBook_Entry__c> listOfEntryData;
        for(BankBook_Entry__c entry : listAccount){
            if(mapOfDateWithEntry.containsKey(entry.Date__c)){
                mapOfDateWithEntry.get(entry.Date__c).add(entry);
            }
            else{
                listOfEntryData = new List<BankBook_Entry__c>();
                listOfEntryData.add(entry);
                mapOfDateWithEntry.put(entry.Date__c,listOfEntryData);
            }
        }
    }

    public dateInPBTbaleControllernew1(ApexPages.StandardController controller){
    
        account = new BankBook_Entry__c();
        totalRevenue = 0;
        totalRevenue1 = 0;
        totalRevenuee = 0;
        totalRevenuee1 = 0;
        
        
       
    }
       
   public void displaingTable(){
        if(account.Date__c != null){
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }
            else {
                Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                for(Date keyDate : allDateSet){
                    if(keydate < account.Date__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.Date__c < keyDate){
                        count ++ ;
                    }
                }
                Date requiredDate ;
                
                if(allDateSet.size() == count){
                    requiredDate = [Select Date__c from BankBook_Entry__c Order by Date__c Desc LIMIT 1][0].Date__c;
                }
                else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);
                
                
            }
        }
       calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2(); 
       
    
    }
     public pagereference calculateTotalRevenue() {
     for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
                }
        for(BankBook_Entry__c acct : listAccount) {
            if(acct.Debit__c!= null)
                totalRevenue+= acct.Debit__c;
                
                }
totalRevenuee1=totalRevenuee+totalRevenue;
         return null;     
       
        
    }
    
    
    
    public void calculateTotalRevenue2() {
        for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
            
              
        }
        
    }
    public void calculateTotalRevenue1() {
        for(BankBook_Entry__c acct1 : listAccount) {
            if(acct1.CB__c!= null)
                totalRevenue1+= acct1.CB__c;
                 
               
        }
        
    }
}
----------------------------------------

<apex:page docType="html-5.0" standardController="BankBook_Entry__c"  extensions="dateInPBTbaleControllernew1" >

<style type="text/css" media="print">
@media print
{
#non-printable { display: none; }
#printable {
display: block;
width: 100%;
height: 100%;
}
}
</style>


        <apex:define name="body">
            
            <div style="width:800px;margin: 0px auto 10px auto;border: 1px solid black;">
    <apex:form >
        <apex:pageBlock >
            
                    <b><apex:outputLabel value="Trial Balance Report On: "  style="font-weight: bold;color:red;"/></b>
                     <apex:inputField value="{!account.Date__c}" style="font-weight: bold;color:red;"/>
                     
                     
                   <!--- Trial Balance Report On: <apex:input type="date" value="{!dat}"/>-->
                 
                 <apex:commandButton value="Display" action="{!displaingTable}" reRender="tableId" style="font-weight: bold;color:red;" oncomplete="window.opener.location.refresh();" />
        <b><apex:commandLink id="printable"  onclick="JavaScript:window.print();" value="Print"/></b> <br/><br/><br/>
                 <apex:pageblockTable value="{!listAccount }" var="record"  id="tableId">
                
                    <apex:column value="{!record.Master_Code1__c}" headerValue="Master Code"/>
                    <apex:column value="{!record.Master_Name__c}" headerValue="Master Name">
                    <apex:facet name="footer" >
                    
                    <apex:outputText value="HDFC CLOSING BALANCE: Rs.{0, number, ###,###,###,##0.00}"  style="font-weight: bold;color:green;font-size:12px;height=18px;"  >
                        <apex:param value="{!totalRevenuee}" />
                   
                     </apex:outputText>
                </apex:facet>
            </apex:column>
                    <apex:column value="{!record.Debit__c}" headerValue="Debit(+)" style="color:green;" >
                     <apex:facet name="footer" >
        
                    <apex:outputText value=" Rs.{!totalRevenue}<br/> Rs.{!totalRevenuee}<br/>-----------------<br/> Rs.{!totalRevenuee1}" style="font-weight: bold;color:green;font-size:12px;height=18px;" escape="false">
                      
                       
                    </apex:outputText>
                </apex:facet>
            </apex:column>
                    <apex:column value="{!record.CB__c}" headerValue="Credit(-)" style="color:red;">
                    <apex:facet name="footer" >
                    <apex:outputText value="Rs.{!totalRevenue1}"  style="font-weight: bold;color:red;font-size:12px;height=18px;"  >
                       
                        
                        
                    </apex:outputText>
                     
                </apex:facet>
                 
            </apex:column>
            
             <!---  <td  style="background-color: #DAFFFF" ><b> <apex:outputText value="Current Total" /></b></td>
           <apex:column value="{!record.Bank_Closing_Balance__c}" headerValue="Bank Closing Balance" style="color:green;">
                    <apex:facet name="footer" >
                    <apex:outputText value="Rs.{0, number, ###,###,###,##0.00}"  style="font-weight: bold;color:green;font-size:12px;height=18px;"  >
                        <apex:param value="{!totalRevenuee}"  />
                    </apex:outputText>
                </apex:facet>
            </apex:column>-->
             
           
                    </apex:pageblockTable>
                            
                    
        </apex:pageBlock>
    </apex:form>
      </div>
        </apex:define>
</apex:page>

 
Chandra Sekhar CH N VChandra Sekhar CH N V
you are returning null in calculateTotalRevenue() method which is of pageReference type.

Also, ensure 'listAccount' has data from the object.
hitesh90hitesh90
Hello,
            
            Try to use following code.

Apex Class:
public with sharing class dateInPBTbaleControllernew1 {
    public BankBook_Entry__c account{get;set;}
    public List<BankBook_Entry__c> listAccount {get;set;}
    public Double totalRevenue {get;set;}
    public Double totalRevenue1 {get;set;}
    public Double totalRevenuee {get;set;}
    public Double totalRevenuee1 {get;set;} 
    public Date dat {get;set;}
    public Map<Date,List<BankBook_Entry__c>> mapOfDateWithEntry = new Map<Date,List<BankBook_Entry__c>>();
    public dateInPBTbaleControllernew1() {
        listAccount  = new List<BankBook_Entry__c>([SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
            FROM BankBook_Entry__c
            order by Master_Code1__c ASC ]);        
        List<BankBook_Entry__c> listOfEntryData;
        for(BankBook_Entry__c entry : listAccount){
            if(mapOfDateWithEntry.containsKey(entry.Date__c)){
                mapOfDateWithEntry.get(entry.Date__c).add(entry);
            }else{
                listOfEntryData = new List<BankBook_Entry__c>();
                listOfEntryData.add(entry);
                mapOfDateWithEntry.put(entry.Date__c,listOfEntryData);
            }
        }
    }
    
    public dateInPBTbaleControllernew1(ApexPages.StandardController controller){    
        account = new BankBook_Entry__c();
        totalRevenue = 0;
        totalRevenue1 = 0;
        totalRevenuee = 0;
        totalRevenuee1 = 0;
    }
    
    public void displaingTable(){
        if(account.Date__c != null){
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }else{
                Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                for(Date keyDate : allDateSet){
                    if(keydate < account.Date__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.Date__c < keyDate){
                        count ++ ;
                    }
                }
                Date requiredDate;                
                if(allDateSet.size() == count){
                    requiredDate = [Select Date__c from BankBook_Entry__c Order by Date__c Desc LIMIT 1][0].Date__c;
                }else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);
            }
        }
        if(listAccount != null && !listAccount.isEmpty()){
            calculateTotalRevenue();
            calculateTotalRevenue1();
            calculateTotalRevenue2(); 
        }    
    }
    public pagereference calculateTotalRevenue() {
        for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
            totalRevenuee= acct2.Bank_Closing_Balance__c;
        }
        for(BankBook_Entry__c acct : listAccount) {
            if(acct.Debit__c!= null)
            totalRevenue+= acct.Debit__c;        
        }
        totalRevenuee1=totalRevenuee+totalRevenue;
        return null;
    }
    public void calculateTotalRevenue2() {
        for(BankBook_Entry__c acct2 : listAccount) {
        if(acct2.Bank_Closing_Balance__c!= null)
            totalRevenuee= acct2.Bank_Closing_Balance__c;        
        }    
    }
    public void calculateTotalRevenue1() {
        for(BankBook_Entry__c acct1 : listAccount) {
        if(acct1.CB__c!= null)
            totalRevenue1+= acct1.CB__c;        
        }        
    }
}

You have to check condition.  "if(listAccount != null)".

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/