Gibran Mohammad Khan
Gibran Mohammad Khan

Reputation: 85

PropertyUtils.setIndexedProperty() throws has no getter method on bean class 'class beans.MyBean'

I have clearly defined the getters and setters of property "listProperty" and my bean class "MyBean.java" is public as well. So why the following code throwing the exception?

public class MyBean
{
    private String stringProperty;
    private float floatProperty;

    //Index Property
    private List<String> listProperty = new ArrayList<>();

    public String getStringProperty() {
        return stringProperty;
    }

    public void setStringProperty(String stringProperty) {
        this.stringProperty = stringProperty;
    }

    public float getFloatProperty() {
        return floatProperty;
    }

    public void setFloatProperty(float floatProperty) {
        this.floatProperty = floatProperty;
    }

    public void setListProperty(List<String> listProperty){
        this.listProperty=listProperty;
    }

    protected List<String> getListProperty(){
        return listProperty;
    }
}

public class BeanUtilsPropertyDemo
{
    public static void main(String[] args) {
        try{
            // Creating the bean and allows to access getter and setter properties
            MyBean myBean = new MyBean();

            // Setting the simple properties on the myBean
            PropertyUtils.setSimpleProperty(myBean,"stringProperty","Hello!This is a string");
            PropertyUtils.setSimpleProperty(myBean,"floatProperty",new Float(25.20));

            //Getting the simple properties
            System.out.println("String Property: "+PropertyUtils.getSimpleProperty(myBean,"stringProperty"));
            System.out.println("Float Property: "+PropertyUtils.getSimpleProperty(myBean,"floatProperty"));

            // Here we will create a list for the indexed property
            List list = new ArrayList();
            list.add("String value 0");
            list.add("String value 1");

            myBean.setListProperty(list);

            //Setting the indexed Property
            PropertyUtils.setIndexedProperty(myBean,"listProperty[1]","This is new string value 1");

            //Getting the indexed Property
            System.out.println("List Property[1]: "+PropertyUtils.getIndexedProperty(myBean,"listProperty[1]"));

        }
        catch(Exception exception){
            exception.printStackTrace();
        }
    }
}

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
String Property: Hello!This is a string
Float Property: 25.2
java.lang.NoSuchMethodException: Property 'listProperty' has no getter method on bean class 'class beans.MyBean'
    at org.apache.commons.beanutils.PropertyUtilsBean.setIndexedProperty(PropertyUtilsBean.java:1667)
    at org.apache.commons.beanutils.PropertyUtilsBean.setIndexedProperty(PropertyUtilsBean.java:1560)
    at org.apache.commons.beanutils.PropertyUtils.setIndexedProperty(PropertyUtils.java:746)
    at test.BeanUtilsPropertyDemo.main(BeanUtilsPropertyDemo.java:32)

Upvotes: 0

Views: 566

Answers (1)

Hassam Abdelillah
Hassam Abdelillah

Reputation: 2294

I think it is just the scope of the getter for listProperty which is protected

Upvotes: 1

Related Questions