【问题标题】:When can ValidatorUtils.getValueAsString() return null?ValidatorUtils.getValueAsString() 什么时候可以返回 null?
【发布时间】:2017-08-17 10:17:26
【问题描述】:

在我们的代码中,我们使用 ValidatorUtils.getValueAsString() 从 bean 中获取字符串形式的值。它在代码执行期间为我们返回了 null。

我在 Apache doc 中找到的方法描述如下:

public static String getValueAsString(Object bean,
                      String property)

从 bean 属性中获取值作为 String 的便捷方法。如果属性是 String[]Collection 并且为空,则返回空的 String ""。否则,返回property.toString()。如果检索属性时出错,此方法可能会返回 null。

哪些情况会导致检索属性出错?

【问题讨论】:

    标签: java apache-commons


    【解决方案1】:

    如果内部使用的PropertyUtils.getProperty(bean, property)发生错误,该方法返回null。

    Javadocs 表示如果发生错误:

    1. beanproeprtynull
    2. bean 不包含 property 的可访问字段或访问器方法
    3. bean 中调用访问器时发生错误

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 2011-04-18
      相关资源
      最近更新 更多