JXLS动态网格抛出null NestedNullException

时间:2017-04-09 08:23:12

标签: jxl jxls

我使用此文档示例作为参考,通过指定标题名称和特定对象所需的属性来创建动态网格。 http://jxls.sourceforge.net/samples/dynamic_grid.html

但在我的情况下,Employee对象具有嵌套属性" adres.housenumber"

public class Employee {
    private String name;
    private Date birthDate;
    private BigDecimal payment;
    private BigDecimal bonus;
    private Adres adres;
    // constructors and getters/setters
    .....
}

public class Adres {
   private String housenumber;
}

如果所有员工的adres都有housenumber,如果员工有adres,那么我的工作正常,那么我就得到以下例外:

  

引起:org.apache.commons.beanutils.NestedNullException:' adres.housenumber'的空属性值在bean类'类Employee'

     

at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:795)~ [commons-beanutils-1.9.3.jar:1.9.3]
  在org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:884)〜[commons-beanutils-1.9.3.jar:1.9.3]
  在org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464)〜[commons-beanutils-1.9.3.jar:1.9.3]
  在org.jxls.command.GridCommand.processBody(GridCommand.java:185)〜[jxls-2.3.0.jar:?]
  在org.jxls.command.GridCommand.applyAt(GridCommand.java:137)〜[jxls-2.3.0.jar:?]
  在org.jxls.area.XlsArea.applyAt(XlsArea.java:168)〜[jxls-2.3.0.jar:?]
  在org.jxls.util.JxlsHelper.processGridTemplate(JxlsHelper.java:156)〜[jxls-2.3.0.jar:?]

从数据库中检索此员工数据,因此在传递到Excel之前,我无权修改它。

有任何想法/帮助吗?

1 个答案:

答案 0 :(得分:0)

问题是由于GridCommand在设置null属性时尝试评估属性的属性。因此,Apache PropertyUtils(由GridCommand使用)会抛出异常。

在您的示例属性中,您的Employee类中的地址为null,因此对adres.housenumber的评估会引发异常。

为了避免这种行为,您可以使用Iterable对象而不是bean。通过这种方式,GridCommand不会评估道具,只是向Iterable对象询问它的​​值。

我已经完成了这个项目,向您展示它是如何工作的。 https://github.com/LucianoZu/jxlsnofault

您可以使用IterableWrapper类来包装Employee并避免异常。