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 - Object obj = new Object(); String objString = String.valueOf(obj); // Calls obj.toString() Object nullObj = null; String nullObjString = String.valueOf(nullObj); // "null" The String.valueOf() method in Java is extremely useful in various scenarios where you need to convert different data types into their string representations.
Discussions

Cleanest way to check for null on a String?
If all you're doing is returning an Optional, use the features of the Optional: return Optional.ofNullable(someObject.get("someKey")); If you need to do some additional processing on the string value, call .map() on the Optional. More on reddit.com
🌐 r/java
11
3
May 8, 2024
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
Is there a logical language-design-type explanation for the following behaviour (Java 7 and I suspect earlier editions as well): Object a = null; String as = String.valueOf(a); // as is assigned "null" System.out.println(as+":"+as.length()); // prints: "null:4" System.out.println ( ... More on stackoverflow.com
🌐 stackoverflow.com
January 2, 2013
🌐
DZone
dzone.com › data engineering › data › string.valueof(object) vs. objects.tostring(object)
String.valueOf(Object) Vs. Objects.toString(Object) - DZone
August 28, 2018 - Although I typically use String.valueOf(Object) instead of Objects.toString(Object) by default when I want the string "null" returned if the passed-in object is null, the alternate overloaded method Objects.toString(Object, String) has the advantage of specifying any string to be returned by the method if the passed-in object is null.
🌐
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 - But as an FYI instead of "String.valueOf" you can use "java.util.Objects.toString(Object o, String nullDefault)" instead, which was added in Java 1.7. If you replaced String.valueOf(someObject.get("someKey")) with Objects.toString(someObject.get("someKey"), null) then it works just fine when the result is null.
🌐
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.
🌐
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.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › valueOf
Java String valueOf() - Convert To String | Vultr Docs
December 9, 2024 - Safely convert this null object using String.valueOf(). java Copy · 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.
🌐
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.
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

🌐
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"
🌐
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
String safeResult = String.valueOf((Object) null); // Returns "null" (no NPE) The decision to return "null" instead of the null reference is intentional and rooted in Java’s design principles of safety and consistency.
🌐
Blogger
marxsoftware.blogspot.com › 2021 › 02 › java-nullpointerexception-avoidance-and.html
Inspired by Actual Events: Java NullPointerException Avoidance and Enhancement Tactics
February 27, 2021 - Allowing Java's implicit string conversion to represent null as the "null" string is the cleanest and easiest way to handle null when constructing strings. However, there are many times when we need a string representation of a Java object when implicit string conversion is not available. In such cases, String.valueOf(Object) can be used to achieve functionality similar to the implicit string conversion.
🌐
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 ...
🌐
javathinking
javathinking.com › blog › why-does-string-valueof-null-throw-a-nullpointerexception
Why Does String.valueOf(null) Throw a NullPointerException in Java? [Documentation vs. Actual Behavior Explained] — javathinking.com
Java developers often rely on String.valueOf() for safe conversion of objects and primitives to their string representations. A common expectation is that passing null to this method would return the string "null", as one might infer from similar ...
🌐
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.