[SalesForce] Visualforce dynamic binding

I have this page:

<apex:page controller="mycontroller">
    <apex:pageBlock >
        <apex:pageblocktable value="{!sc2.records}" var="test">
    <!--<apex:pageblocktable value="{!acclist}" var="test"> Know this is possible-->
            <apex:column headervalue="check" value="{!test['name']}"> </apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

and this class

public class mycontroller {
    public apexpages.standardsetcontroller sc2{get;set;}
    //public list<account> acclist{get;set;}
    public mycontroller()
    {    
        sc2 = new apexpages.standardsetcontroller([select name from account limit 10]);
        //acclist = (list<account>)sc2.getrecords();
    }
}

I am trying to directly use standardsetcontroller.records directly in visualforce page.
But it gives me an error

Error Error: Read access denied for null

I know we can typecast sobject to specifc one and rather use that in vf page
I am just trying to see whats issue here

Best Answer

when you access the standardSetController records collection you get back an sObject array.. and when you bind that to pageblock table, you will be iterating over sObject at a time... and the only field you can access from a generic sObject is Id..

reference https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SObjects_accessing_fields.htm

If you use the generic sObject type instead of a specific object, such as Account, you can retrieve only the Id field using dot notation. You can set the Id field for Apex code saved using Salesforce.com API version 27.0 and later). Alternatively, you can use the generic sObject put and get methods.

to access any other fields of the sObject you need to do the typecasting..

to test that, if you change the apex column value to display the Id, it should work..

<apex:page controller="mycontroller">
    <apex:pageBlock >
        <apex:pageblocktable value="{!sc2.records}" var="test">
            <apex:column value="{!test.Id}"></apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

but based on the dynamic visualforce syntax to retrieve the fields from sObject, your syntax looks perfectly fine and it should work..

when i tried your code without the headerValue attribute it works perfectly fine.. and when i add the headerValue attribute back it results in the same error.

final code that worked for me

<apex:page controller="mycontroller">
    <apex:pageBlock >
        <apex:pageblocktable value="{!sc2.records}" var="test">
            <apex:column value="{!test['Name']}"></apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>