JDK-4867608 : String.valueOf(null) throws NullPointerException instead of returning "null"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.1,1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2003-05-21
  • Updated: 2003-05-23
  • Resolved: 2003-05-23
Description

Name: gm110360			Date: 05/21/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :

Invoking the method String.valueOf() with the argument null fails.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just invoke the String.valueOf() method with null. Do not use a reference to null.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The method should just return a string containing "null"
ACTUAL -
The method throws a NullPointerException

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.<init>(String.java:165)
        at java.lang.String.valueOf(String.java:2191)
        at Test.main(Test.java:5)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

public class Test {

    public static void main(String[] args) {
        String str = String.valueOf(null);
        System.out.println(str);
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :

The usage of a reference to null works as expected. The following programm works:

public class Test {

    public static void main(String[] args) {
        Object o = null;
        String str = String.valueOf(o);
        System.out.println(str);
    }
}
(Review ID: 185373) 
======================================================================

Comments
EVALUATION 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
23-05-2003