Java Bean Specifications for boolean field

笑着哭i 提交于 2019-12-10 16:45:56

问题


I have Java bean with the field activeRecord

private Boolean activeRecord;

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

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

when I send it in List as Jasper Report data source

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

I get the error message

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'

Why Jasper doesn't recognize the isActiveRecord as a getter method?


回答1:


The prefix is... can be used for methods that return a primitive boolean. However, your field activeRecord is of type Boolean, which is an object (the wrapper type of boolean), and for objects you always need to use get....

From the 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.

As such, you have two possible fix:

  • Make your activeRecord a boolean and keep the getter isActiveRecord(). This would be the preferred approach if activeRecord cannot be null.
  • Keep it as a Boolean, but rename your method isActiveRecord() to getActiveRecord(). You'll need to make sure the caller handles null properly.


来源:https://stackoverflow.com/questions/39307379/java-bean-specifications-for-boolean-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!