8

I have a wrapper class that I want to reuse for Both Accounts and Contacts

public class SelectableSObject{
    public Boolean selected {get; set;}
    public SObject obj {get; set;}
}

In the constructor something like this...

selectableAccounts = new List<SelectableSObject>();
for(Account acc : [SELECT Name FROM Account]){
   SelectableSObject so = new SelectableSObject();
   so.selected = false;
   so.obj = acc;
   selectableAccounts.add(so);
}

When I try to bind a field in visual force:

<apex:pageBlock>
    <apex:dataTable value="{!selectableAccounts}" var="a">
        <apex:column >
            <apex:inputCheckbox value="{!a.selected}" />
        </apex:column>
        <apex:column headervalue="Account Name" value="{!a.obj['name']}"/>
    </apex:dataTable>
</apex:pageBlock>

I get a non-descriptive compile error:

Read access denied for null (Line: 1)

Seems like this should be possible, but maybe its not?

  • can you post your pageblock/ repeat / datatable and how you populate the a variable?Are you trying to get the name field of the sobject? – Rao Jun 5 '14 at 23:13
  • @rao done. I'm just trying to access the name field as an example. Get the same error if I try to access Id or any other fields as far as i can tell. – NSjonas Jun 6 '14 at 0:01
  • @Ralph, I have never used Apex and VF before its my first day and I am given a task to finish in a week time... If you dont mind can you explain me your example. So I can start woking on my task. Thank you so much for the example at the first place. – NewBei Sep 16 '14 at 13:15
11

This works fine for me. Ran a couple tests and all of the below work.

VF Page

<apex:page controller="GenericTest">
    {!test['name']}
    {!test1.obj['name']}
    <apex:repeat value="{!testList}" var="test2">
        {!test2.obj['name']}
    </apex:repeat>
    <apex:pageBlock>
        <apex:pageBlockTable value="{!testList}" var="test3">
            <apex:column>{!test3.obj['name']}</apex:column>
            <apex:column value="{!test3.obj['name']}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Controller

public with sharing class GenericTest {

    public Sobject test { get; set; }
    public Wrap test1 { get; set; }
    public List<Wrap> testList { get; set; }

    public GenericTest() {
        test = new Account(name = 'asdf');
        test1 = new Wrap();
        test1.obj = test;
        testList = new List<Wrap>{ test1 };
    }

    public class Wrap {
        public SObject obj { get; set; }
    }
}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.