StringBuffer sb=null; // Some more logic that conditionally assigns value to the StringBuffer // Prints Value=null System.out.println(\"Value=\"+sb); // Throws Nu
Let's bracket the expression the way that the compiler effectively would, in the broken vase:
System.out.println( ("Value" + sb != null) ? sb.toString() : "Null");
Now "Value" + sb will never be null even if sb is null... so when sb is null, it's calling toString() and going bang.
"Value" + sb
sb
toString()