0

I am trying to display all search results in a wrapper class, but there's this error showing up every time I save the controller:

Error: Compile Error: Constructor not defined: [CustomSettingController.ObjWrapper].(Object__c)

The error points to this line of code: oWrapper.add(new ObjWrapper(o));

Please take note that I omitted the search button from the visualforce page as I am trying to get rid of the error for now...

Apex Class:

public class CustomSettingController {

    public CustomSetting custSet {get; set;}
    public List<Object__c> objList {get;set;}
    public Integer rowToRemove {get;set;}
    public List<ObjWrapper> oWrapper {get;set;}
    String userinput; 

    public CustomSettingController() {
        String custSetId = ApexPages.currentPage().getParameters().get( 'id' );

        if (custSetId != null ) {
            custSet = new CustomSetting([SELECT Id, Name, Role_Name__c FROM Setting1__c WHERE Id = :custSetId]);
        } else {
            custSet = new CustomSetting();
        }
    }

    public String getuserinput(){return userinput;}
    public void setuserinput(String userinp){this.userinput=userinp;}

    public List<SelectOption> getUserRoleList() {
        List<UserRole> userRoleList = [SELECT Id, Name FROM UserRole];
        List<SelectOption> uRoleOptionList = new List<SelectOption>();
        uRoleOptionList.add(new SelectOption(' ',' '));

        for(UserRole ur : userRoleList) {
            uRoleOptionList.add(new SelectOption(ur.Name, ur.Name));    
        }

        return uRoleOptionList;
    }

    public void search() {

        List<ObjWrapper> oWrapper = new List<ObjWrapper>();
        for(Object__c o : [SELECT Id, Name, Field1__c, Field1__r.Name FROM Object__c WHERE Field1__r.Name LIKE :userinput+'%']) {
            oWrapper.add(new ObjWrapper(o));    
        }

        return null;

    }

    public List<ObjWrapper> getResults() {

        return oWrapper; 

    }

    public void addNewRowToObjList() {

        List<ObjWrapper> temp_objWrapper = new List<ObjWrapper>();
        ObjWrapper newObjWrap = new ObjWrapper();
        Object__c newO = new Object__c();
        newObjWrap.obj = newO;
        newObjWrap.index = temp_objWrapper.size();
        temp_objWrapper.add(newObjWrap);

        for(ObjWrapper owrap : temp_objWrapper) {
            oWrapper.add(owrap);    
        }    

    }

    public void removeRowFromObjList() {

        List<ObjWrapper> rem_objWrapper = new List<ObjWrapper>();

        for(ObjWrapper owrap : oWrapper) {
            rem_objWrapper.add(owrap);    
        }

        rem_objWrapper.remove(rowToRemove); 
        oWrapper.clear();

        for(ObjWrappe owrap2 : rem_objWrapper) {
            oWrapper.add(owrap2);    
        }        

    }

    public class CustomSetting {
        public Id Id {get;set;}
        public String Name {get;set;}
        public String selectedRole {get;set;}

        public CustomSetting(Setting1__c s1) {
            this.Id = s1.Id;
            this.Name = s1.Name;
            this.selectedRole = s1.Role_Name__c;
        }

        public CustomSetting() {}

        public Setting1__c getSetting1() {
            return new Setting1__c(
                Id = this.Id,
                Name = this.Name,
                Role_Name__c = this.selectedRole

            );
        }

    }

    public class ObjWrapper {

        public Integer index {get;set;}
        public Object__c obj {get;set;}

    }
}

Visualforce Page:

<apex:page controller="CustomSettingController" sidebar="false" showHeader="false">
    <apex:pageMessages />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Setting Details">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Name</apex:outputLabel>
                    <apex:inputText value="{!custSet.Name}"/>                
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Role Name</apex:outputLabel>
                    <apex:selectList value="{!custSet.selectedRole}" size="1">
                        <apex:selectOptions value="{!UserRoleList}"/> 
                    </apex:selectList>                
                </apex:pageBlockSectionItem>     
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Line Item Details">
                <apex:pageBlockSectionItem >
                    <apex:inputText value="{!userinput}" label="Input"/>
                </apex:pageBlockSectionItem>

                <apex:outputPanel id="tablepanel">
                <apex:variable value="{!0}" var="rowNum"/>
                <apex:pageBlockTable title="Search Results" value="{!oWrapper}" var="ow">
                    <apex:column headerValue="Action">
                        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromObjList}" rendered="{!rowNum > 0}" rerender="tablepanel" immediate="true">
                            <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
                        </apex:commandLink>
                        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
                    </apex:column>
                    <apex:column value="{!ow.obj.Name}"/>    
                    <apex:column value="{!ow.obj.Field1__r.Name}"/>
                </apex:pageBlockTable>
                <apex:commandButton value="Add New Item" action="{!addNewRowToObjList}" reRender="tablepanel"/>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

1 Answer 1

3

You are passing the Object__c variable directly, but the wrapper class is not having any constructor to assign variable o to obj in wrapper class.

Try setting the variable like this,

oWrapper.add(new ObjWrapper(obj=o));

Or

Add a constructor in wrapper class like this,

public class ObjWrapper {

   public Integer index {get;set;}
   public Object__c obj {get;set;}

   public ObjWrapper(Object__c o) {
      this.obj = o;
   }
}

In this way it will know variable o needs to be assigned where.

Note: Default constructor is not a parameterized constructor.

3
  • I put the constructor in the wrapper class. The error on the line I have provided disappeared, but it got transferred to another line which is this line: ObjWrapper newObjWrap = new ObjWrapper(); from the addNewRowToObjList method
    – Gaynerys
    Commented Feb 17, 2018 at 9:33
  • 1
    Try adding a constructor without parameter too. public ObjWrapper() { } Means try having both the constructors Commented Feb 17, 2018 at 9:42
  • Error's gone! Now I understand. Thanks for your help :)
    – Gaynerys
    Commented Feb 17, 2018 at 9:44

You must log in to answer this question.

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