2

I am practicing salesforce and stuck with an error. Can someone help me out?

Error:

Incompatible key type Set for Map at line 23 column 6

Apex Class:

public class mapapex {
    public map<string,string> mst;
    public list<string> lst {get;set;}
    public set<string> str  {get;set;}
    public set<string> mod {get;set;}
    public list<string> s{get;set;};
    public mapapex(){
       mst= new map<string,string>();
       mst.put('India','rupee');
       mst.put('USA','Doller');
       mst.put('Japan','yen');  
    }
    public pagereference showkey(){
       str=mst.keyset();
       return null;
    }
    public pagereference showvalue(){
        lst=mst.values();
       return null;
    }
    public pagereference showmod(){
        s = mst.get(mod);
        return null;
    }        
}

VF Page:

<apex:page controller="mapapex" >
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="showkeys" action="{!showkey}" rerender="one"/>
            <apex:dataList id="one" value="{!str}" var="a"> {!a} </apex:dataList>
            <apex:commandButton value="showvalue" action="{!showvalue}" rerender="two"/>
            <apex:dataList id="two" value="{!lst}" var="b"> {!b} </apex:dataList>
            <apex:outputlabel> Enter the value for key- India </apex:outputlable>
            <apex:inputText value="{!mod}"/>
            <apex:outputlabel> Value for key India is {!s}</apex:outputlable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

3 Answers 3

3

You have defined mod in your controller to be a Set. And it looks like in your page you are trying to pass in a String.

Change:

public set<string> mod {get;set;}

To:

public String mod {get;set;}
2
  • Hi Jesse..I have updated the code..could you please advice if you solutions still works with this update.
    – Yusuf Syed
    Commented Mar 15, 2016 at 19:43
  • @YusufSyed Did you change what I pointed out? There may be other issues in the code, I was simply answering why you are getting an Incompatible key type error. As, that was your question. Commented Mar 15, 2016 at 19:51
2

You have to do few changes

  1. Modify the declaration of
public set<string> mod {get;set;}
public list<string> s{get;set;}

to

public string mod {get;set;}
public string s{get;set;}

because those are actually string variables and not collection as your intention is to get the mod from user input as key and get the corresponding value from the map.

  1. In your VF page, there is no code to invoke the showmod() method. So modify
<apex:outputlabel> Value for key India is {!s}</apex:outputlabel>

to

<apex:outputlabel id="three"> Value for key India is {!s}</apex:outputlabel>

and add

<apex:commandButton value="showmod" action="{!showmod}" rerender="three"/>

Hope this helps.

4
  • Thanks Vignesh...Though i did't represent my problem and purpose here in a good way..you got what my intention was with this code..thank you.
    – Yusuf Syed
    Commented Mar 16, 2016 at 10:50
  • Welcome Yousuf. Is there anything I could be of help? Commented Mar 16, 2016 at 10:57
  • Thanks Vignesh... no as of now...will post if needed.
    – Yusuf Syed
    Commented Mar 16, 2016 at 11:38
  • If the provided solution solved your issue please mark it as answer so that others with same issue gets benefited. Commented Mar 16, 2016 at 11:41
1

@Yusuf, There is also need to change in the following line:

Change:

s = mst.get(mod);

To:

s.add(mst.get(mod));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .