Originally posted by Andry Dub:
I can't understand this behaviour
It's the same thing as the original example. If you pass "null" to println() (and thus to String.valueOf()), it is properly handled. String.valueOf(null) returns "null", as a special case.
If, on the other hand, you pass an Object whose toString() method returns null, then valueOf() calls toString() on that object, gets a null value that it
doesn't expect, and throws an NPE.
So to summarize: valueOf() can handle a null argument, but it can't handle an object that returns null from toString(). Writing toString() that returns null is an evil thing to do, in any case.