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
Anuj Thakur 4Anuj Thakur 4 

Displaying records in VF page

Hi All,
I am trying to display the Account records on Visual Force Page where Phone and fax fields values are equal. 
I have written a Controller and design a VF page. But when I am clicking on "Display records" Button then I am getting below error.
Visualforce Error
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!display}' in component <apex:commandButton> in page displayrecords: Class.DispalyMatchingRecords.display: line 9, column 1
Class.DispalyMatchingRecords.display: line 9, column 1
I tried to debug this, Actually while adding the value in "acc" list its throwing the error.
Could anyone help me out here. Actually I am not having much knowledge of VF pages so not sure where I am doing wrong.

VF Page:- 
<apex:page controller="DispalyMatchingRecords" >
    <apex:form>
    <apex:pageBlock>
        <apex:pageBlockTable value="{!acc}" var="a">
        <apex:column value= "{!a.Id}"/>
            <apex:column value= "{!a.Name}"/>
        </apex:pageBlockTable>
<apex:commandButton value="Display Records" action="{!display}"/>  
    </apex:pageBlock>
        </apex:form>
</apex:page>

Controller:-
public class DispalyMatchingRecords {

   public List<Account> acc {get;set;}
    public void display(){
        List<Account> ac =[Select Id, Name, Phone,Fax from Account];
        for(Account a: ac){
            if(a.Phone != null && a.Fax != null){
                if(a.Phone == a.Fax){
                    acc.add(a);
                }
            }
        }
    }
}
Best Answer chosen by Anuj Thakur 4
sfdcMonkey.comsfdcMonkey.com
HI update your apex class with following code :
public class DispalyMatchingRecords {

   public List<Account> acc {get;set;}
    public void display(){
        acc = new List<Account>();
        List<Account> ac =[Select Id, Name, Phone,Fax from Account];
        for(Account a: ac){
            if(a.Phone != null && a.Fax != null){
                if(a.Phone == a.Fax){
                    acc.add(a);
                }
            }
        }
    }
}
i have added following line 
acc = new List<Account>();
Thanks let us know if it helps you 
sfdcMonkey.com

 

All Answers

sfdcMonkey.comsfdcMonkey.com
HI update your apex class with following code :
public class DispalyMatchingRecords {

   public List<Account> acc {get;set;}
    public void display(){
        acc = new List<Account>();
        List<Account> ac =[Select Id, Name, Phone,Fax from Account];
        for(Account a: ac){
            if(a.Phone != null && a.Fax != null){
                if(a.Phone == a.Fax){
                    acc.add(a);
                }
            }
        }
    }
}
i have added following line 
acc = new List<Account>();
Thanks let us know if it helps you 
sfdcMonkey.com

 
This was selected as the best answer
Steven NsubugaSteven Nsubuga
acc had not been initialized.
Try this
public class DispalyMatchingRecords {

   public List<Account> acc {get;set;}
    public void display(){
        List<Account> ac =[Select Id, Name, Phone,Fax from Account];
        acc = new List<Account>();
        for(Account a: ac){
            if(a.Phone != null && a.Fax != null){
                if(a.Phone == a.Fax){
                    acc.add(a);
                }
            }
        }
    }
}

 
Anuj Thakur 4Anuj Thakur 4
Hi Piyush/Steven,
Thanks :) 
Its worked. that was a silly mistake done by me. 
Thanks a lot .
Regards,
Anuj
Anuj Thakur 4Anuj Thakur 4
Sure.