JDK-4326988 : API: SimpleDateFormat throws NullPointerException when parsing with null pattern
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.3.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2000-03-31
  • Updated: 2001-08-08
  • Resolved: 2001-08-08
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.
Other
1.4.0 beta2Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description

Name: stC104175			Date: 03/31/2000


java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)



1) Just run and see.

2) a test source....

import java.text.*;

public class SDFTest
{
	
	public static void main (String[] args)
	{
		String dateFormat = null;
		try
		{
			// Note: the dateFormat is a 'null'.
			// This -was- becourse it should come from a database
			// and returnd a null object.
			// The workaround is just build in the -normal- null
check.
			SimpleDateFormat df = new SimpleDateFormat (dateFormat);
			df.parse("01-01-2000");
		}
		catch (ParseException e)
		{
			System.out.println ("[SDFTest] Unparsable date");
		}
		catch (Exception e)
		{
			System.out.println ("[SDFTest] Another Exception.");
			e.printStackTrace();
		}
	}
}

3) The error message:

c:\java\jdk1.3\bin\java.exe SDFTest
[SDFTest] Another Exception.
java.lang.NullPointerException
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:640)
	at java.text.DateFormat.parse(DateFormat.java:329)
	at SDFTest.main(SDFTest.java:16)

4) Info:
This is not only -not- working on 1.3 but also on 1.1.8 and 1.2.2

5) Configuration...
On different windows (nt4) configurations this will get a nullpointer...
(Review ID: 103143) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin-beta2 FIXED IN: merlin-beta2 INTEGRATED IN: merlin-beta2
08-07-2004

WORK AROUND Name: stC104175 Date: 03/31/2000 check for null before creating the SimpleDateFormat ======================================================================
08-07-2004

SUGGESTED FIX CCC request: 4326988: API: SimpleDateFormat throws NullPointerException when parsing with null pattern Release: Merlin Problem: The java.text.SimpleDateFormat constructors and applyPattern method don't check a given pattern for null or invalid syntax and then the format or parse method fails later. The normal design of java.text.Format subclasses is that errors in their setup are caught during setup, and that the parse and format methods only fail if their own input is invalid. MessageFormat, which uses other Format classes as subformatters, relies on this behavior for its own error handling. The pattern validation should be performed before setting a pattern. Requesters: HOT Objects, Norbert Lindenberg, Masayoshi Okutsu Proposed API change: 1. SimpleDateFormat.format() will no longer throw IllegalArgumentException. Remove the following in SimpleDateFormat.format(): * @exception IllegalArgumentException if this * <code>SimpleDateFormat</code>'s pattern string is invalid and add the following: * @exception NullPointerException if the given date is null 2. Define exceptions in the SimpleDateFormat constructors and applyPattern() as follows: 1) Add the following to SimpleDateFormat(String pattern), SimpleDateFormat(String pattern, Locale locale) and applyPattern(String pattern). * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid 2) Add the following to readObject(ObjectInputStream stream). * @exception InvalidObjectException if the pattern is invalid. API reviewed and approved by: Norbert Lindenberg Implementation: - Engineer who made (or will make) the changes: Masayoshi Okutsu - Date at which changes will be complete: 7/27/01 - Number of lines of new or modified code: + Java: 35 + Native: 0 - Code reviewed (or will be reviewed) by: Norbert Lindenberg Risk assessment: Low risk. (Currently programs fail in format() or parse() if a null or invalid pattern is used in SimpleDateFormat. Impact to existing applications will be small.) SQE (product testing) impact; contact ###@###.###: A regression test will be provided by the engineering. - Name of person who approved change and committed to add/modify tests: Min-Chi tien <###@###.###> JCK (compatibility testing) impact; contact jck-ccc@eng: New test items for exceptions will be required. - Name of person who approved change and committed to write JCK tests: "Andrey Y. Chernyshov" <###@###.###> Doc impact (contact Alan Sommerer if you don't know your contact): The API doc changes above are considered sufficient as documentation. Reviewed and approved by Alan Sommerer. Localization impact: No - If yes, describe: Internationalization impact: No - If yes, describe: Security impact: No - If yes, describe: Compatibility impact: No - If yes, describe: Legal impact: No - If yes, describe: For feature changes, Product Marketing approval: N/A ###@###.### 2004-06-17
17-06-2004

EVALUATION The SimpleDateFormat constructors, which take pattern, should throw NullPointerException rather than failing in the parse() method. masayoshi.okutsu@Eng 2000-04-04 It should also check the pattern for correctness, so InvalidArgumentException is thrown here instead of in parse() or format() or other methods that interpret the pattern later on. Throwing the exceptions later on is inconsistent with the specifications of these methods in Format, which only allows these exceptions if the object to be formatted or the string to be parsed are invalid, not if the Format object has been set up incorrectly. Note also that MessageFormat specifies that its pattern is checked in applyPattern, which in turn requires that SimpleDateFormat checks its pattern in the constructor. norbert.lindenberg@Eng 2001-06-27
27-06-2001