gpt4 book ai didi

java - 获取 java.lang.NoSuchMethodException : Property 'xx' has no setter method in class 'class xx' while using PropertyUtils. setSimpleProperty 函数

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:22 26 4
gpt4 key购买 nike

我正在使用 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. Caused by: java.lang.NoSuchMethodException: Property 'reportName' has no setter method in class 'class FileDt' at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

  2. Caused by: java.lang.NoSuchMethodException: Property 'reportLocation' has no setter method in class 'class FileDt' at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

最佳答案

PropertyUtilsBean.setSimpleProperty(Object bean, String name, Object value))仅适用于公共(public)方法。看起来您的类使用包范围(类定义中缺少 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)

让你的类(class)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://");
}
}

关于java - 获取 java.lang.NoSuchMethodException : Property 'xx' has no setter method in class 'class xx' while using PropertyUtils. setSimpleProperty 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com