访问 bean 的嵌套/索引属性时如何防止 NPE

发布于 2024-08-31 08:50:15 字数 266 浏览 7 评论 0原文

使用 commons-beanutils 访问嵌套 bean 时,有什么方法可以防止 NPE 吗? 这是我的代码:

new BeanUtilsBean().getProperty(human, "parent.name");

在这种情况下,我希望 getProperty()human.getParent() == null 时返回空字符串 ("") 或在除了抛出 NPE 之外的其他方式。

Is there any way to prevent NPE when accessing a nested bean using commons-beanutils?
Here is my code:

new BeanUtilsBean().getProperty(human, "parent.name");

In this case I want getProperty() to either return empty string ("") when human.getParent() == null or handle it in a way other that throwing an NPE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-09-07 08:50:15

他们正在考虑添加语言功能JDK7,但是 最终它们没有被添加

现在你必须手动检查。你可以破解它并创建一个类似

public static void propertyHack(Object bean, String property, String nullreplace){
  try{
    return new BeanUtilsBean().getProperty(bean, property);
  }
  catch(NullPointerException npe){
    return nullreplace;
  }
}

“有点糟糕”的函数,但它会起作用。

They were thinking of adding language features to JDK7, but ultimately they weren't added

For now you'll have to manually check. You can just hack it and create a function like

public static void propertyHack(Object bean, String property, String nullreplace){
  try{
    return new BeanUtilsBean().getProperty(bean, property);
  }
  catch(NullPointerException npe){
    return nullreplace;
  }
}

Kind of sucks, but it will work.

染柒℉ 2024-09-07 08:50:15

PropertyUtils 有一个用于嵌套属性 getNestedProperty(...) 的特定方法,它通过抛出 NestedNullException 来处理 NPE,这可能(?)更好为了眼睛。

这是 Javadoc

PropertyUtils has a specific method for nested properties getNestedProperty(...) that handles NPE by throwing a NestedNullException, which is probably(?) better for the eye.

Here is the Javadoc.

萧瑟寒风 2024-09-07 08:50:15

如果其他人正在寻找答案

    Guia g = new Guia();
    GuiaParticipante gp = new GuiaParticipante(1);
    g.setTbGuiaParticipanteCollection(Collections.singletonList(gp));//comment this line to test
    String name = "tbGuiaParticipanteCollection[0].codParticipante";//the expression itself
    Resolver resolver = new DefaultResolver();//used to "clean" the expression
    if (resolver.isIndexed(name)) {
        String property = resolver.getProperty(name);//remove the [0].codParticipante

        if (PropertyUtils.getProperty(g, property) != null) { //get the collection object, so you can test if is null
            String cod = BeanUtils.getNestedProperty(g, name); //get the value if the collection isn't null
            System.out.println(cod);
        }
    } 

If someone else is searching the answer

    Guia g = new Guia();
    GuiaParticipante gp = new GuiaParticipante(1);
    g.setTbGuiaParticipanteCollection(Collections.singletonList(gp));//comment this line to test
    String name = "tbGuiaParticipanteCollection[0].codParticipante";//the expression itself
    Resolver resolver = new DefaultResolver();//used to "clean" the expression
    if (resolver.isIndexed(name)) {
        String property = resolver.getProperty(name);//remove the [0].codParticipante

        if (PropertyUtils.getProperty(g, property) != null) { //get the collection object, so you can test if is null
            String cod = BeanUtils.getNestedProperty(g, name); //get the value if the collection isn't null
            System.out.println(cod);
        }
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文