`
zhaoningbo
  • 浏览: 609533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多
引言:

  今天同事发现我封装的SQL分离,有个很2B的异常——“NullPointerException”。丢人丢大发了,留个爪印,以后用新方法得先留神说明书了~。=

正文:

  直接上测试代码:
public class XMLTypeDemo {

    public static void main(String[] args) {

        String str = "我们的灵魂都应该是自由的,哪怕有圣洁与污秽之分。";
        
        // true
        System.out.println(str.contains("自由"));
        // java.lang.NullPointerException
        System.out.println(str.contains(null));
    }
}

  根本原因在String.contains的实现是这么回事:
/**
     * Returns true if and only if this string contains the specified
     * sequence of char values.
     *
     * @param s the sequence to search for
     * @return true if this string contains <code>s</code>, false otherwise
     * @throws NullPointerException if <code>s</code> is <code>null</code>
     * @since 1.5
     */
    public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;
    }

  注释上已经说明用法了。就不再浪费大伙带宽了~~。+


4
1
分享到:
评论
4 楼 yuhui0531 2012-03-08  
zhaoningbo 写道
yuhui0531 写道
果然2,非常2,相当2,十分2,真是2B

哈哈~~~ 恩恩,引以为戒

哈哈,和你开玩笑呢。。
其实我也不晓得。
3 楼 Mybeautiful 2012-03-07  
如果你不了解这个新方法,又不想花时间深入了解。就默认它没有做null处理..
2 楼 zhaoningbo 2012-03-06  
yuhui0531 写道
果然2,非常2,相当2,十分2,真是2B

哈哈~~~ 恩恩,引以为戒
1 楼 yuhui0531 2012-03-06  
果然2,非常2,相当2,十分2,真是2B

相关推荐

Global site tag (gtag.js) - Google Analytics