JDK-8074102 : DateFormat must throw a NullPointerException for setCalendar and setNumberFormat
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 8u31
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_8
  • CPU: x86
  • Submitted: 2015-02-15
  • Updated: 2017-05-19
  • Resolved: 2016-11-09
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 9
9Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

A DESCRIPTION OF THE PROBLEM :
currently DateFormat does not throw a NullPointerException when the parameter to setCalendar or setNumberFormat is null. This causes subsequent calls to the format method to fail

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Consider the following class:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Bug {

	public static void main(String[] args) throws ParseException {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		fmt.setCalendar(null);
		fmt.setNumberFormat(null);
		fmt.format(new Date());
	}

}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect DateFormat to prevent callers from moving it into an illegal state. Both
fmt.setCalendar(null);
and
fmt.setNumberFormat(null);
must throw a NullPointerException in the code above
ACTUAL -
A NullPointerException is thrown at the line
fmt.format(new Date());
because its required internal variables were set to null

REPRODUCIBILITY :
This bug can be reproduced always.


Comments
It is a duplicate of JDK-8169480
09-11-2016

Checked this for JDK 8, 8u31, 8u40 ea b23, 8u60 ea b04, and 9 ea b50 and could reproduce the runtime exception. >java Bug Exception in thread "main" java.lang.NullPointerException at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936) at java.text.DateFormat.format(DateFormat.java:345) at Bug.main(Bug.java:12)
02-03-2015