Question

Error:

Cannot make a static reference to the non-static method setSelectedInfoRole(SelectOption) from the type PeopleInfoViewModel.PeopleInfoData

Here's code:

    @Command
    @NotifyChange({ "peopleInfoList", "selectedFolderInfo" })
    public void encryptInfo() {
        Set selectedPeopleInfo=peopleInfoList.getSelection();
        if (selectedPeopleInfo == null) {
            showError(pageResourceBundle.getText("PLS_SELECT_AN_INFO_TO_CRYPT"));
            return;
        } 
        List<PeopleInfoRecord> folderRecord = new ArrayList<PeopleInfoRecord>();


        for (Object selectedObj : selectedPeopleInfo) {
            if (selectedObj instanceof PeopleInfoData) {
                PeopleInfoData peopleInfoData = (PeopleInfoData) selectedObj;
                ValidInfoRow validInfoRow = Services.folderService().findValidInfoByInfoCode(peopleInfoData.getPeopleInfoRecord().getInfoCode());

                if (InfoType.CHOOSE.equals(validInfoRow.getInfoType())) {
                    if (!CsdcStringUtils.isBlank(peopleInfoData.getPeopleInfoRecord().getInfoValue(), true)) {
                        PeopleInfoData.setSelectedInfoRole(new SelectOption<String>(peopleInfoData.getPeopleInfoRecord().getInfoValue()));
                    }
                }

            }
        }
    }

In above method line

   PeopleInfoData.setSelectedInfoRole(new SelectOption<String>(peopleInfoData.getPeopleInfoRecord().getInfoValue()));

give error.

Here's Pojo class:

public class PeopleInfoData {
        private ListModelList<SelectOption<String>> infoobj;

        private PeopleInfoRecord peopleInfoRecord;
        private SelectOption<String> selectedInfoRole;


        public SelectOption<String> getSelectedInfoRole() {
            return selectedInfoRole;
        }

        public void setSelectedInfoRole(SelectOption<String> selectedInfoRole) {
            this.selectedInfoRole = selectedInfoRole;
        }

        public ListModelList<SelectOption<String>> getInfoobj() {
            return infoobj;
        }

        public void setInfoobj(ListModelList<SelectOption<String>> infoobj) {
            this.infoobj = infoobj;
        }

    }
Was it helpful?

Solution

setSelectedInfoRole() is a non-static method and you're trying to call it in a static manner PeopleInfoData.setSelectedInfoRole which is wrong. You need to create a object of that class and then call that method.

new PeopleInfoData().setSelectedInfoRole(new SelectOption<String>(peopleInfoData.getPeopleInfoRecord().getInfoValue()));

But it looks as that you already have a object created for that class. You need to use that object to call this method.

PeopleInfoData peopleInfoData = (PeopleInfoData) selectedObj;
peopleInfoData.setSelectedInfoRole(new SelectOption<String>(peopleInfoData.getPeopleInfoRecord().getInfoValue()));

OTHER TIPS

Your problem in this line: PeopleInfoData.setSelectedInfoRole(new SelectOption<String>(peopleInfoData.getPeopleInfoRecord().getInfoValue())) is the capital 'P'. Your variable is called peopleInfoData You're trying to access the method like it's a static method of PeopleInfoData.

See the Java Language Specification, section 8.4.3.2.

A static class method implies that it is a general utility method for that class, not specific to actual objects of that class. A static method, among other things, cannot refer to this, as yours does.

I expect you meant to write peopleInfoData on that line instead of PeopleInfoData, so you would refer to the object itself, and not its class. Otherwise, Java will not know which object this you are referring to when this method gets called.

You're using the class name instead of the variable name. Just use a lowercase "p" and you'll be fine. You can't make a static reference (using the class name) to a non static method. You need to use an instance of that object to access a non static method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top