StringBuffer sb=null;
// Some more logic that conditionally assigns value to the StringBuffer
// Prints Value=null
System.out.println(\"Value=\"+sb);
// Throws Nu
I think the issue is that the statement is being parsed like this:
System.out.println( ("Value="+sb) != null ? sb.toString() : "Null" );
The string concatenation operator (+) has a heigher precedence than the ternary operator.
Since "Value"+null is always not null, sb.toString() will always be called, even if sb is null, hence the NullPointerException.
If in doubt - parenthesize! Even if not in doubt! :)