gpt4 book ai didi

java - 字符串实用程序中可能出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 05:50:07 26 4
gpt4 key购买 nike

我正在使用 commons StringUtils.isNotBlank() 来检查字符串是否不为空和空白。在某些情况下,对于空白字符串或 null,因为输入验证没有按预期发生。 Null 或空白正在通过。

   if( StringUtils.isNotEmpty(obj.getvalue()) ){
.............
}

当我通过 documentation of StringUtils我发现,

A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils (except for deprecated methods).

这是什么意思?跟我的场景有关系吗?

最佳答案

阅读你引用的上面的句子:

StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method.

A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils (except for deprecated methods).

因此,由于它处理空值,因此不应抛出 NullPointerExceptions。

如果检查方法 isNotBlank,您会发现此表:

 StringUtils.isNotBlank(null)      = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true

如果您收到其他值,您确实遇到了错误,您应该报告。但更有可能的是,NPE 是由其他原因引起的。例如,可能其他一些 StringUtils 方法返回 null。

关于java - 字符串实用程序中可能出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com