United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6463998 : Undocumented NullPointerException from Float.parseFloat and Double.parseDouble

Details
Type:
Bug
Submit Date:
2006-08-25
Status:
Resolved
Updated Date:
2012-03-20
Project Name:
JDK
Resolved Date:
2009-07-31
Component:
core-libs
OS:
linux,windows_7
Sub-Component:
java.lang
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6,6u29
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

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



Hardware and Software, Engineered to Work Together