Why does String.valueOf(null) throw a NullPointerException?

98,411

Solution 1

The issue is that String.valueOf method is overloaded:

Java Specification Language mandates that in these kind of cases, the most specific overload is chosen:

JLS 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.

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"

Related questions


Moral of the story

There are several important ones:

  • Effective Java 2nd Edition, Item 41: Use overloading judiciously
    • Just because you can overload, doesn't mean you should every time
    • They can cause confusion (especially if the methods do wildly different things)
  • Using good IDE, you can check which overload is selected at compile time
    • With Eclipse, you can mouse-hover on the above expression and see that indeed, the valueOf(char[]) overload is selected!
  • Sometimes you want to explicitly cast null (examples to follow)

See also


On casting null

There are at least two situations where explicitly casting null to a specific reference type is necessary:

  • To select overloading (as given in above example)
  • To give null as a single argument to a vararg parameter

A 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"

See also

Related questions

Solution 2

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));

Solution 3

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

Share:
98,411
user282886
Author by

user282886

Updated on April 12, 2022

Comments

  • user282886
    user282886 about 2 years

    according to the documentation, the method String.valueOf(Object obj) returns:

    if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

    But how come when I try do this:

    System.out.println("String.valueOf(null) = " + String.valueOf(null));
    

    it throws NPE instead? (try it yourself if you don't believe!)

        Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.(Unknown Source)
        at java.lang.String.valueOf(Unknown Source)
    

    How come this is happening? Is the documentation lying to me? Is this a major bug in Java?

  • Joachim Sauer
    Joachim Sauer almost 14 years
    Another suggestion: When you overload a method and one argument could be called on two overloads (such as null in this case), make sure that both overloads act the same with regard to that value!
  • polygenelubricants
    polygenelubricants almost 14 years
    @Joachim: I just read the item, and was pleasantly surprised to find that these two methods were explicitly discussed! Bloch went a step further and claim that since String.valueOf(Object) and valueOf(char[]) do completely different things anyway (irrespective of null or not) that perhaps they shouldn't have been overloads in the first place.
  • user972276
    user972276 over 8 years
    Got a question... if you have a method that returns an Object then the statement return null; would be the exact same thing as return (Object) null;?