1

I am trying to create a class using the following code but getting error: " Incompatible element type String for collection of listex.s "

Page:

<apex:page controller="listex">
<apex:pageBlock >
     <apex:pageBlockTable value="{!list}" var="a">
         <apex:column value="{!a.numb}"/>
         <apex:column value="{!a.str}"/>
     </apex:pageBlockTable>
 </apex:pageBlock>
</apex:page>

Controller:

public class listex {
    public list<s> inte;
    public integer numb;
    public string str;

    public list<s> getlist(){
        list<s> inte = new list<s>();
        for(integer i=0;i<3;i++) {
            inte.add(numb=i,str='name'+i);
            return inte;
        }
    }

    public class s{
        public integer numb;
        public string str;
        public s(integer i, string s){
            this.numb = i;
            this.str = s;
        }
    }
}

2 Answers 2

3

The problem is on this line

inte.add(numb=i,str='name'+i);

you are trying to add an element of type s to the collection inte but you need to call the constructor as part of the list add

inte.add(new s(i,'name'+i);
3
  • Hi, After making that change, I am getting error: "Unknown property 'listex.s.numb'"
    – sf_user
    Commented Apr 30, 2015 at 1:20
  • Never mind, I got it. Forgot to add get,set inside class.
    – sf_user
    Commented Apr 30, 2015 at 1:21
  • this is presumably a VF error? In your inner class s, the members that are exposed to the VF page need to be declared with {get; set;}
    – cropredy
    Commented Apr 30, 2015 at 1:22
2

Your next problem is that you need getters and setters on your wrapper class. Try this:

public class s{
    public integer numb {get;set;}
    public string str {get;set;}
    public s(integer i, string s){
        this.numb = i;
        this.str = s;
    }
}
0

You must log in to answer this question.

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