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:

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

  • How does polymorph ambiguity distinction work?
  • Which overload will get selected for null in Java?

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

  • Polymorphism vs Overriding vs Overloading
  • Method Overloading. Can you overuse it?

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

  • Java Language Guide/varargs - to understand how it's implemented

Related questions

  • Why null cast?
  • Difference between double… and double[] in formal parameter type declaration
Answer from polygenelubricants on Stack Overflow
Top answer
1 of 4
224

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:

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

  • How does polymorph ambiguity distinction work?
  • Which overload will get selected for null in Java?

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

  • Polymorphism vs Overriding vs Overloading
  • Method Overloading. Can you overuse it?

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

  • Java Language Guide/varargs - to understand how it's implemented

Related questions

  • Why null cast?
  • Difference between double… and double[] in formal parameter type declaration
2 of 4
20

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));
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - String.valueOf(Object obj): Converts an object to a string. If the object is null, it returns the string "null".
Discussions

apex - Why does valueOf not work with NULLs sometimes? - Salesforce Stack Exchange
While this will work - I would have to add this to every valueOf with a null input would also yield a bulk output. ... The author of the String.valueOf chose to allow a null argument (presumably generating the string 'null'). More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
Why "null" String is returned in String.valueOf instead of java null? - Stack Overflow
I was checking String.valueOf method and found that when null is passed to valueOf it returns "null" string instead of pure java null. My question is why someone will return "null" string why not ... More on stackoverflow.com
🌐 stackoverflow.com
Java String.valueOf(null) throws NPE, but Object a = null; String.valueOf(a) returns 'null' - Stack Overflow
It's important to note that it's a compile error if there is no single overloading that is more specific than all the others - if there were a valueOf(String) method as well as valueOf(Object) and valueOf(char[]) then a call to the untyped String.valueOf(null) would be ambiguous. More on stackoverflow.com
🌐 stackoverflow.com
January 2, 2013
java - Why String.valueOf(null) is causing null pointer exception? - Stack Overflow
This give a "null" string. but ... Because String.valueOf(null) picks the overloaded method with char[] argument, and then fails in the new String(null) constructor. More on stackoverflow.com
🌐 stackoverflow.com
November 21, 2011
🌐
Reddit
reddit.com › r/java › cleanest way to check for null on a string?
Cleanest way to check for null on a String? : r/java
May 8, 2024 - D is correct by chance : you switch the String.valueOf() call to obj.toString and I had to look the doc "if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned." If it was a pure replacement I would raise an eyebrow during code review, but that code is 100% correct.
🌐
DZone
dzone.com › data engineering › data › string.valueof(object) vs. objects.tostring(object)
String.valueOf(Object) Vs. Objects.toString(Object) - DZone
August 28, 2018 - Both methods String.valueOf(Object) and Objects.toString(Object) essentially do the same thing: call the toString() method on a passed-in object. This is the case as long as it's not null or it doesn't return the string "null" when null is passed to them. In short, both methods are designed to provide a simple approach for invoking an object's toString() without worry about a NullPointerException if it turned out to be null.
🌐
Baeldung
baeldung.com › home › java › java string › object.tostring() vs string.valueof()
Object.toString() vs String.valueOf() | Baeldung
April 7, 2025 - String.valueOf() and Object.toString() provide similar results, but we use them differently. The static String,valueOf(), allows us to pass all sorts of data and return a string with null safety. The Object.toString() is for us to override and provide a text representation of the instance.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › strings › .valueof()
Java | Strings | .valueOf() | Codecademy
July 26, 2025 - When you pass a null object to .valueOf(), it returns the string “null” instead of throwing an exception.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_valueof_object.htm
Java.lang.String.valueOf() Method
Following is the declaration for java.lang.String.valueOf() method ... If the argument is null, then a string equal to "null", else the value of obj.toString() is returned.
🌐
Java String
javastring.net › home › java string valueof() method examples
Java String valueOf() Method Examples
August 6, 2019 - jshell> String nullStr = String.valueOf(null); | Exception java.lang.NullPointerException | at String.<init> (String.java:260) | at String.valueOf (String.java:3056) | at (#42:1) jshell> Object obj = null; obj ==> null jshell> String nullStr = String.valueOf(obj); nullStr ==> "null"
Top answer
1 of 3
44

In statement System.out.println(String.valueOf(null)); there is a call of method public static String valueOf(char data[]), which source code is as follows:

public static String valueOf(char data[]) {
  return new String(data);
}

That is why you get NPE

On the other hand, in statement Object a = null; String as = String.valueOf(a); there is a calls of method public static String valueOf(Object obj), which source code is as follows:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

That is why you get "null" instead of NPE


A bit of theory from Java Language Specification: 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 of type Object, but not all Object are of type char[]. Type char[] is more specific than Object and as described in the Java Language Specification, the String.valueOf(char[]) overload is chosen in this case.

EDIT

It is also worth mentioning what Ian Roberts mentioned (in his comment below):

It's important to note that it's a compile error if there is no single overloading that is more specific than all the others - if there were a valueOf(String) method as well as valueOf(Object) and valueOf(char[]) then a call to the untyped String.valueOf(null) would be ambiguous

2 of 3
15

The first invocation is to String#valueOf(Object), the second is to String#valueOf(char[])

The overloaded method is chosen according to the argument's static type, this is why the first works and the seconds get an NPE.

If you invoke System.out.println ( String.valueOf((Object)null)); it will work

🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › valueOf
Java String valueOf() - Convert To String | Vultr Docs
December 9, 2024 - Object nullObj = null; String nullStr = String.valueOf(nullObj); System.out.println(nullStr); // Prints "null" Explain Code · This technique ensures no NullPointerException is thrown and "null" is returned as a string, showcasing the method's safety in handling null values.
Top answer
1 of 1
18

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.

🌐
javaspring
javaspring.net › blog › why-null-string-is-returned-in-string-valueof-instead-of-java-null
Why Does String.valueOf Return 'null' String Instead of Java null? Explained
For primitives, valueOf() simply ... interesting: if the object is non-null, it calls obj.toString(); if the object is null, it returns the string "null"....
🌐
Coderanch
coderanch.com › t › 522022 › java › parse-null-string-null
How to parse null string to null. (Beginning Java forum at Coderanch)
December 30, 2010 - Hi, Is there a method that will parse - given string "null" to null, - will leave other string values untouched, - and given value null will also remain null. Basically a more readable equivalent of: I noticed String.valueOf(Object o) returns "null" explicitly when given object o is null, so ...
🌐
CodingTechRoom
codingtechroom.com › question › understanding-string-valueof-why-does-it-return-null-instead-of-java-null-
Why Does String.valueOf Return "null" Instead of a Java Null? - CodingTechRoom
In Java, the String.valueOf() method is designed to handle different types of inputs, including null. When you pass a null reference to this method, it converts it to the string literal "null" instead of returning the Java null value.
🌐
Baeldung
baeldung.com › home › java › java string › integer.tostring() vs string.valueof() in java
Integer.toString() vs String.valueOf() in Java | Baeldung
April 7, 2025 - Integer.toString() might throw NullPointerException if given Integer object is null. String.valueOf() won’t throw an exception because it’ll go to String.valueOf(Object obj) method and return null. Please note, primitive int passed to String.valueOf(int i) can never be null but since there is another method String.valueOf(Object obj), we can get confused between the two overloaded methods.