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.
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.
How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.
A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.
The issue is that String.valueOf
method is overloaded:
String.valueOf(Object)
String.valueOf(char[])
Java Specification Language mandates that in these kind of cases, the most specific overload is chosen:
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.
A char[]
is-an Object
, but not all Object
is-a char[]
. Therefore, char[]
is more specific than Object
, and as specified by the Java language, the String.valueOf(char[])
overload is chosen in this case.
String.valueOf(char[])
expects the array to be non-null
, and since null
is given in this case, it then throws NullPointerException
.
The easy "fix" is to cast the null
explicitly to Object
as follows:
System.out.println(String.valueOf((Object) null));
// prints "null"
There are several important ones:
valueOf(char[])
overload is selected!null
(examples to follow)null
There are at least two situations where explicitly casting null
to a specific reference type is necessary:
null
as a single argument to a vararg parameterA simple example of the latter is the following:
static void vararg(Object... os) {
System.out.println(os.length);
}
Then, we can have the following:
vararg(null, null, null); // prints "3"
vararg(null, null); // prints "2"
vararg(null); // throws NullPointerException!
vararg((Object) null); // prints "1"
The problem is that you're calling String.valueOf(char[])
and not String.valueOf(Object)
.
The reason for this is that Java will always choose the most specific version of an overloaded method that works with the provided parameters. null
is a valid value for an Object
parameter, but it's also a valid value for a char[]
parameter.
To make Java use the Object
version, either pass in null
via a variable or specify an explicit cast to Object:
Object o = null;
System.out.println("String.valueOf(null) = " + String.valueOf(o));
// or
System.out.println("String.valueOf(null) = " + String.valueOf((Object) null));
A bug, numbered 4867608 was filed for this way back in 2003, which was resolved as "won't fix" with this explanation.
We can't change this due to compatibility constraints. Note that it is the public static String valueOf(char data[]) method which ends up being invoked and it does not mention the replacement of "null" for null arguments.
@###.### 2003-05-23
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