Why String.valueOf(null) is causing null pointer exception? Where the expected behaviour is to return "null" string.
String x = null;
System.out.println(String.valueOf(x));
This give a "null" string. but
System.out.println(String.valueOf(null));
will cause null pointer exception.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
The String. valueOf(Object) method, as its Javadoc-generated documentation states, returns "null" if the passed in object is null and returns the results on the passed-in Object 's toString() call if the passed-in Object is not null.
The null-pointer exception error occurs in my toString method. I'm at a loss as to why. The error can occur through multiple ways. Most commonly, an object's reference is declared but the object itself remains uncreated.
Because String.valueOf(null)
picks the overloaded method with char[]
argument, and then fails in the new String(null)
constructor. This choice is made at compile time.
If you want to explicitly use the overloaded method with a Object
argument, use:
String.valueOf((Object) null)
Note that there is no overloaded method taking a String
argument - the one invoked in the first case is taking Object
.
To quote the JLS:
15.12.2 Compile-Time Step 2: Determine Method Signature
The second step searches the type determined in the previous step for member methods. This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable, that is, declarations that can be correctly invoked on the given arguments. There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch.
All of the methods are applicable, so we go to:
15.12.2.5 Choosing the Most Specific Method
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
Thanks to polygenelubricants - there are only two overloaded methods accepting an object - char[]
and Object
- char[]
is most specific.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With