+ Start a Discussion
Rabbani sayyed 8Rabbani sayyed 8 

I have a wrapper class, please find the below code and can any one tell me why i am getting this error

<apex:page controller="wrapperClassExample">
    <apex:pageBlock title="Account from Wrapper class">
        <apex:pageBlockTable value="{!wraccount}" var="wra">
            <apex:column value="{!wra.acc.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Opportunity from wrapper class">
        <apex:pageBlockTable value="{!wropportunity}" var="wro">
            <apex:column value="{!wro.opp.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Controller:
--------------

public class wrapperClassExample{
public list<wrapperClass> wraplist{get;set;}     
public list<wrapperClass> getwraccount()
{
    list<Account>newacclist=[select id,name from Account limit 3];
    wraplist=new list<wrapperClass>();  
                                  
    for(Account acn:newacclist)
    {
        wraplist.add(new wrapperClass(acn));   
    }
    return wraplist;
 }
    
    public list<wrapperClass> getwropportunity()
    {
        list<Opportunity>opplist=[select id,name from opportunity limit 20];
        wraplist=new list<wrapperClass>();
        for(Opportunity op:opplist)
        {
            wraplist.add(new wrapperClass(op));  
        }
        return wraplist;
    
    }
    
    public class wrapperClass{
    public Account acc{get;set;}
    public Opportunity opp{get;set;}
    public wrapperClass(Account accn){   
        acc=accn;
    }
    public wrapperClass(Opportunity oppn){   
        opp=oppn; 
    }
    }

 }
 
Error:
--------
Error: wrapperClassExample Compile Error: Illegal assignment from List<Account> to List<Account> at line 6 column 5

Can anyone help me out this error where i have modify
 
Sagar PareekSagar Pareek
In your organization somewhere you have created a class named "Account". Thats why the compiler is not able to understand that it is standard Object Account or the class created by you. Please rename your class, it will work.

Or try
List<Schema.Account> lst;

 
Rabbani sayyed 8Rabbani sayyed 8
Hi sagar

i am getting another error...please find the below error

Error: wrapperClassExample Compile Error: Loop variable must be of type Account at line 9 column 17

 
Sagar PareekSagar Pareek
Please use Schema.Account in loop also.

 
ManojjenaManojjena
Hi Rabbani ,

Please check with below code .
public class wrapperClassExample{
	public list<wrapperClass> wraplist{get;set;}
	public list<wrapperClassAccount> wraplistAcc{get;set;}
	public list<wrapperClassAccount> getwraccount(){
		list<Account>newacclist=[select id,name from Account limit 3];
		wraplist=new list<wrapperClass>();  
		wraplistAcc=new List<wrapperClassAccount>();                             
		for(Account acn:newacclist){
			wraplist.add(new wrapperClassAccount(acn));   
		}
		return wraplist;
	}
	public list<wrapperClass> getwropportunity(){
		list<Opportunity>opplist=[select id,name from opportunity limit 20];
		wraplist=new list<wrapperClass>();
		for(Opportunity op:opplist){
			wraplist.add(new wrapperClass(op));  
		}
		return wraplist;
	}
	public class wrapperClass{
		public Opportunity opp{get;set;}
		public wrapperClass(Opportunity oppn){   
		opp=oppn; 
		}
	}
	public class wrapperClassAccount{
		public Account acc{get;set;}
		public wrapperClassAccount(Account ac){
		acc=ac;
		}
	}
}

<apex:page controller="wrapperClassExample">
    <apex:pageBlock title="Account from Wrapper class">
        <apex:pageBlockTable value="{!wraplistAcc}" var="wra">
            <apex:column value="{!wra.acc.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Opportunity from wrapper class">
        <apex:pageBlockTable value="{!wropportunity}" var="wro">
            <apex:column value="{!wro.opp.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Thnaks 
Manoj