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
kantravikantkantravikant 

Visualforce Error : Map key 1011 not found in map

controller class is as

 

public class demo
{
    public map<String,String>demoMap{get;set;}
    public List<String> demoList {
        get {return new List<String> {'1011','1111','1112','112'};}set;}

    public demo()
    {
        demoMap=new map<String,String>();
        demoList=new list<String>();
    }

    public boolean demoMethod()

    {
        demoMap=new map<String,String>();
        for(String s :demoList )
        {
            String key=s;
            String value='test';
            demoMap.put(key,value);
        }
        return true;
    }

}

 

 

visualforce page is as

 

<apex:page controller="demo"> 
<apex:panelGrid columns="4">
<apex:outputLabel value="{!demoMap[demoList[0]]}"/>
<apex:outputLabel value="{!demoMap[demoList[1]]}"/>
<apex:outputLabel value="{!demoMap[demoList[2]]}"/>
<apex:outputLabel value="{!demoMap[demoList[3]]}"/>
</apex:panelGrid>
</apex:page>

 

path2cloudpath2cloud

demoMethod() is not ebign called, so demoMap doesnt contain data, call it in  constructor, I dint undertsand why demoMethod() return type is boolean.

 

public class demo
{
    public map<String,String>demoMap{get;set;}
    
    public List<String> demoList {
        get {return new List<String> {'1011','1111','1112','112'};}set;}

    public demo()
    {
        demoMap=new map<String,String>();
        demoList=new list<String>();
        demoMethod(); /*************************call method here ******************/
    }

    public boolean demoMethod()

    {
        demoMap=new map<String,String>();
        system.debug('*********************** '+demoList);
        for(String s :demoList)
        {
            String key=s;
            String value='test';
            demoMap.put(key,value);
        }
        return true;
    }

}

kantravikantkantravikant

Thanks path2cloud.

 

The most important is sequence of expression in constructor.

eg.

public demo()
    {
        demoMap=new map<String,String>();
        demoList=new list<String>();
        demoMethod(); /*************************call method here ******************/
    }

 

in above example.