正在获取 java.lang.NoSuchMethodException:属性 'xx' 在 class 'class xx' 中没有 setter 方法,同时使用 PropertyUtils.setSimpleProperty 函数
Getting java.lang.NoSuchMethodException: Property 'xx' has no setter method in class 'class xx' while using PropertyUtils.setSimpleProperty function
我正在使用 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://");
}
}
这两种方法都抛出异常
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)
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 方法。看起来你的 class 使用了包范围(在 class 定义中缺少 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://");
}
}
我正在使用 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://");
}
}
这两种方法都抛出异常
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)
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 方法。看起来你的 class 使用了包范围(在 class 定义中缺少 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://");
}
}