Java 布尔字段的 Bean 规范

Java Bean Specifications for boolean field

我有 Java 个字段为 activeRecord

的 bean
private Boolean activeRecord;

@Override
public Boolean isActiveRecord() {
    return activeRecord;
}

@Override
public void setActiveRecord(Boolean activeRecord) {
    this.activeRecord = activeRecord;
}

当我将其作为 Jasper Report 数据源发送到 List

List<Branch> dataList = new BranchLogic().selectAll();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

我收到错误消息

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: activeRecord. .... Caused by: java.lang.NoSuchMethodException: Property 'activeRecord' has no getter method in class 'class com.tawaak.app.data.domain.model.branch.Branch'

为什么 Jasper 不能将 isActiveRecord 识别为 getter 方法?

前缀 is... 可用于 return 原语 boolean 的方法。但是,您的字段 activeRecord 是类型 Boolean,它是一个对象(boolean 的包装类型),对于对象,您始终需要使用 get....

来自JavaBeans specification, 8.3.2:

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This is<PropertyName> method may be provided instead of a get<PropertyName> method, or it may be provided in addition to a get<PropertyName> method.

因此,您有两种可能的解决方法:

  • 让你的 activeRecord 成为 boolean 并保持 getter isActiveRecord()。如果 activeRecord 不能是 null.
  • ,这将是首选方法
  • 将其保留为 Boolean,但将方法 ​​isActiveRecord() 重命名为 getActiveRecord()。您需要确保来电者正确处理 null