[b]引言:[/b]
今天同事发现我封装的SQL分离,有个很2B的异常——“NullPointerException”。丢人丢大发了,留个爪印,以后用新方法得先留神说明书了~。=
[b]正文:[/b]
直接上测试代码:
根本原因在String.contains的实现是这么回事:
注释上已经说明用法了。就不再浪费大伙带宽了~~。+
今天同事发现我封装的SQL分离,有个很2B的异常——“NullPointerException”。丢人丢大发了,留个爪印,以后用新方法得先留神说明书了~。=
[b]正文:[/b]
直接上测试代码:
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;
}
注释上已经说明用法了。就不再浪费大伙带宽了~~。+