JDK-8261290 : Improve error message for NumberFormatException on null input
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 11
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2021-02-05
  • Updated: 2021-04-06
  • Resolved: 2021-02-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17
17 b11Fixed
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Long.valueOf(null);

Exception in thread "main" java.lang.NumberFormatException: null
    at java.base/java.lang.Long.parseLong(Long.java:655)
    at java.base/java.lang.Long.valueOf(Long.java:1144)
    at Main.main(Main.java:3)

This error message is a bit hard to decipher without understand that the Exception message IS the value passed in (null).  It just looks like there is no message with this Exception.  It would be better (and seems to be the current trend) if this method was more cleat in the JavaDocs about 'null' and also that it threw an explicit NPE (Objects#requireNonNull).



Comments
From the submitter: Thank you for reaching out to me. I appreciate the help. https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Long.java#L684-L686 https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Long.java#L763-L765 I am not sure if you are able to change the Exception classes here, but in one instance of "parseLong" it throws a NPE (via Objects.requireNonNull). It would be helpful to have that symmetry in the other instance of "parseLong" if at all possible. If the method may not be altered to throw a NPE, then this message is certainly more clear.
12-03-2021

Requested the submitter to verify the fix with the latest version of JDK at https://jdk.java.net/17/
10-03-2021

Changeset: 564011cf Author: Joe Darcy <darcy@openjdk.org> Date: 2021-02-21 18:43:26 +0000 URL: https://git.openjdk.java.net/jdk/commit/564011cf
21-02-2021

A kindness to users to have a more explicit message for a null input.
20-02-2021

I agree changing the method to throw NPE is not warranted, but a slightly more verbose message might be helpful, such as "null string argument".
09-02-2021

In the abstract, having Long.valueOf(null) throw NullPointerException seems reasonable. However, throwing NumberFormatException is explicitly handled in the code and is likely intentional behavior. Furthermore, it's likely that the calling code is catching and handling NFE for all cases of malformed input, including null, so changing the behavior to throw NPE is likely to result in incompatibilities. Closing as Won't Fix.
08-02-2021

Moved to JDK to discuss more details.
08-02-2021