Getting java.lang.NoSuchMethodException: the 'xx' property does not have a setter method in the 'class xx' class when using the PropertyUtils.setSimpleProperty function

I use PropertyUtils.setSimpleProperty to dynamically call the setter method, but for some reason I keep getting an error. I need your help to find out the reason. Here is my code:

class FileDt {
    String reportName=null;
    String reportLocation=null;

    public String getReportName() {
        return reportName;
    }
    public void setReportName(String reportName) {
        this.reportName = reportName;
    }
    public String getReportLocation() {
        return reportLocation;
    }
    public void setReportLocation(String reportLocation) {
        this.reportLocation = reportLocation;
    }
}

class Foo {
    public static void main (String... args) {
        FileDt dt = newFileDt();
        // #1
        PropertyUtilsBean.setSimpleProperty(dt, "reportName", "abc.html");
        // #2
        PropertyUtilsBean.setSimpleProperty(dt, "reportLocation", "c://");
    }
}

Both of these methods throw an exception.

  • Called: java.lang.NoSuchMethodException: The 'reportName' property does not have a setter method in the FileDt class of class org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty (PropertyUtilsBean.java:2096)

  • : java.lang.NoSuchMethodException: 'reportLocation' setter FileDt org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

+4
1

PropertyUtilsBean.setSimpleProperty(Object bean, String name, Object value)) . , ( public ).

:

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;

class FileDt {
    String reportName;
    String reportLocation;

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    public String getReportLocation() {
        return reportLocation;
    }

    public void setReportLocation(String reportLocation) {
        this.reportLocation = reportLocation;
    }

    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        FileDt dt = new FileDt();
        // #1
        PropertyUtils.setSimpleProperty(dt, "reportName", "abc.html");
        // #2
        PropertyUtils.setSimpleProperty(dt, "reportLocation", "c://");
    }
}

, :

Exception in thread "main" java.lang.NoSuchMethodException: Property 'reportName' has no setter method in class 'class FileDt'
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)
    at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:928)
    at FileDt.main(FileDt.java:28)

public :

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;

public class FileDt {
    String reportName;
    String reportLocation;

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    public String getReportLocation() {
        return reportLocation;
    }

    public void setReportLocation(String reportLocation) {
        this.reportLocation = reportLocation;
    }

    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        FileDt dt = new FileDt();
        // #1
        PropertyUtils.setSimpleProperty(dt, "reportName", "abc.html");
        // #2
        PropertyUtils.setSimpleProperty(dt, "reportLocation", "c://");
    }
}
+3

Source: https://habr.com/ru/post/1684184/


All Articles