StringBuffer sb=null;
// Some more logic that conditionally assigns value to the StringBuffer
// Prints Value=null
System.out.println("Value="+sb);
// Throws NullPointerException
System.out.println("Value=" + sb != null ? sb.toString() : "Null");
的工作修复了这个问题涵盖括号三元运算符:奇怪的空指针异常情况:三元条件运算符不能与字符串连接
// Works fine
System.out.println("Value=" + (sb != null ? sb.toString() : "Null"));
这怎么可能?
在此之后,我感到很沮丧。 – Sid 2010-07-19 09:29:49
@SidCool我刚刚有完全相同的问题,谢谢你已经问我这个问题:) – Nick 2012-09-05 13:19:41
欢迎您,尼克! – Sid 2012-09-06 03:55:05