JDK-6463998 : Undocumented NullPointerException from Float.parseFloat and Double.parseDouble
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6,6u29
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux,windows_7
  • CPU: x86
  • Submitted: 2006-08-25
  • Updated: 2017-05-16
  • Resolved: 2009-07-31
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 7
7 b68Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux localhost 2.6.17-1mdk #1 Fri Jul 21 09:47:28 CDT 2006 i686 Intel(R) Pentium(R) 4 CPU 2.66GHz Mandriva Linux


A DESCRIPTION OF THE PROBLEM :
When a null value is passed to Float.parseFloat() or Double.parseDouble(), a NullPointerException is thrown, which is not mentioned in the API Specifications. Additionally, this exception is wrapped in a NumberFormatException if Integer.parseInt() or Long.parseLong() is used. Wouldn't it be better if these methods handle this the same way?

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See the code below

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Integer: java.lang.NumberFormatException: null
Long: java.lang.NumberFormatException: null
Float: java.lang.NumberFormatException: null
Double java.lang.NumberFormatException: null
ACTUAL -
Integer: java.lang.NumberFormatException: null
Long: java.lang.NumberFormatException: null
Float: java.lang.NullPointerException
Double java.lang.NullPointerException

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test {
	
	
	public static void main(String[] args) {
		
		try {
			Integer.parseInt(null);
		} catch (Exception ex) {
			System.out.println(ex);
		}
		
		try {
			Long.parseLong(null);
		} catch (Exception ex) {
			System.out.println(ex);
		}
		
		try {
			Float.parseFloat(null);
		} catch (Exception ex) {
			System.out.println(ex);
		}
		
		try {
			Double.parseDouble(null);
		} catch (Exception ex) {
			System.out.println(ex);
		}
		
	} // end main
	
	
} // end class
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
We need to catch an additional NullPointerException to ensure the reliability of our code.

Comments
SUGGESTED FIX # http://hg.openjdk.java.net/jdk7/tl/jdk/rev/2a1b1075f583 # HG changeset patch # User darcy # Date 1247694193 25200 # Node ID 2a1b1075f583e0bd454e03a32c8bb18218fcdbd6 # Parent aaf0cb20646e4519dc7ca10e869f37b91d353f1c 6463998: Undocumented NullPointerExeption from Float.parseFloat and Double.parseDouble Reviewed-by: lancea, iris --- a/src/share/classes/java/lang/Double.java Wed Jul 15 12:08:55 2009 -0700 +++ b/src/share/classes/java/lang/Double.java Wed Jul 15 14:43:13 2009 -0700 @@ -529,6 +529,7 @@ public final class Double extends Number * @param s the string to be parsed. * @return the {@code double} value represented by the string * argument. + * @throws NullPointerException if the string is null * @throws NumberFormatException if the string does not contain * a parsable {@code double}. * @see java.lang.Double#valueOf(String) --- a/src/share/classes/java/lang/Float.java Wed Jul 15 12:08:55 2009 -0700 +++ b/src/share/classes/java/lang/Float.java Wed Jul 15 14:43:13 2009 -0700 @@ -438,12 +438,13 @@ public final class Float extends Number * represented by the specified {@code String}, as performed * by the {@code valueOf} method of class {@code Float}. * - * @param s the string to be parsed. + * @param s the string to be parsed. * @return the {@code float} value represented by the string * argument. - * @throws NumberFormatException if the string does not contain a + * @throws NullPointerException if the string is null + * @throws NumberFormatException if the string does not contain a
2009-07-15

EVALUATION While the reported issue is valid, it should not be surprising that a method would throw a NullPointerException when given a null pointer even if it is not documented to do so.
2006-08-25