获取java.lang.NoSuchMethodException:使用PropertyUtils.setSimpleProperty函数时,属性'xx'在类'class xx'中没有setter方法

时间:2017-08-22 04:40:13

标签: java javabeans nosuchmethoderror

我正在使用PropertyUtils.setSimpleProperty动态调用我的setter方法,但由于某种原因我不断收到错误。需要你的帮助来找出根本原因。这是我的代码:

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://");
    }
}

这两种方法都抛出异常

  1.   

    引起:java.lang.NoSuchMethodException:属性'reportName'       在类'FileDt'中没有setter方法       org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

  2.   

    引起:java.lang.NoSuchMethodException:属性'reportLocation'   在类'FileDt'中没有setter方法   org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

1 个答案:

答案 0 :(得分:3)

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://");
    }
}