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
prasanth kumarprasanth kumar 

getting in while using map in visualforce page

hi,  i am getting this error while saving the code in visualforce page.  This error is caused due to of "{!accts[a].name}"  in VF apge.

Error:-  

Error: /apex/retunrtype: java.lang.NullPointerException
Error: null
<apex:page controller="allaccountslist">
 
 <apex:repeat value="{!accts}" var="a">
 {!a.id}............... {!accts[a].name} <br/><br/>
 </apex:repeat>
 
</apex:page>
 
public class allaccountslist {

public list<sobject> getaccts ()
{
    list<sobject> acc= [select id,name from account limit 20];
    
    return acc;

}}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Accts is list not map So got the value from List you need to pass the index in Number not in ID. Like below
{!a.id}............... {!accts[1].name} <br/><br/>

Option 1:- Value from List
Try below code if you want to get value from index.
<apex:page controller="allaccountslist">
 
     <apex:variable value="{!0}" var="rowNum"/>
     <apex:repeat value="{!accts}" var="a">
         {!a.id}............... {!accts[rowNum].name} <br/><br/>
         <apex:variable var="rowNum" value="{!rowNum + 1}"/>
     </apex:repeat>
 
</apex:page>

Option 2:- If you want to use Map then
Change your class like below :- return type should  me Map<Id,Account>
public class allaccountslist 
{

    public Map<Id,Account> getaccts ()
    {
        Map<Id,Account> acc= new Map<Id,Account>( [select id,name from account limit 20] ) ;
        return acc;
    
    }
}

Page lik below
<apex:page controller="allaccountslist">
 
     <apex:repeat value="{!accts}" var="a">
         {!a}............... {!accts[a].name}<br/><br/>
     </apex:repeat>
 
</apex:page>

Please let us know if this will help you

Thanks
Amit Chaudhary