Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:32:28+00:00 2026-05-15T12:32:28+00:00

according to the documentation, the method String.valueOf(Object obj) returns: if the argument is null

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:32:28+00:00Added an answer on May 15, 2026 at 12:32 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the msdn documentation : GetPictureFromObject method Returns a picture (Visual Basic Picture
According to the documentation for NSString's method -UTF8String : The returned C string is
According to the documentation for -[UIView setNeedsLayout] : Because this method does not force
According to Android documentation, using the .getTime() method in the Android location will return
according documentation: On success, the function returns the converted integral number as a long
According to the documentation: When this method is first called, it creates a single
According to the documentation of the == operator in MSDN , For predefined value
I create a Popup using the PopupFactory.getPopup method. According to the documentation, I am
According to jQuery's .data() documentation you can use the .data() method to data prefixed
According to MSDN Documentation for partial classes : Partial methods are implicitly private So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.