function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chandramohan Yetukuri 1Chandramohan Yetukuri 1 

Error while retrieving a value from apex class to VF page.

I have a simple apex class like this. I am trying to retrieve a string value from apex class in my vf page(Find below). I am getting the error
"Error: Unknown property 'String.empName"
Please help to resolve the below issue.

Apex Class:-
public class constructorClass {
    
    public string empName;
    public String getempDetails()
    {   
        empName = 'Chandra';
        return empName;
        
    }
}


VF Page:-
=======
<apex:page controller="constructorClass"> <apex:form > <apex:pageBlock > <apex:pageBlockSection title="TestConstructor"> <apex:pageBlockTable value="{!empDetails}" var="t"> <apex:column value="{!t.empName}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Shiva RajendranShiva Rajendran
Hi ChandraMohan,

​Use
<apex:pageBlockTable value="{!empDetails}" var="t"> <apex:column value="{!t}"/>

OR you could use
    public string empName{get;set;} instead of public string empName;

in this case the code didnt work because  in function 
  
public String getempDetails()
    {   
        empName = 'Chandra';
        return empName;
        
    }



you have returned a reference to empName variable. but since that variable didnt have get method , you couldn't display in vf page.
If you have used just return 'Chandra' the code would have worked
Let me know if you need further help.

Thanks and Regards,
Shiva RV

 
Chandramohan Yetukuri 1Chandramohan Yetukuri 1
Oh!! Thanks a lot for explanation Shiva Rajendran.