JDK-8210739 : Calling JSpinner's setFont with null throws NullPointerException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 9,11,12
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2018-09-07
  • Updated: 2019-08-16
  • Resolved: 2018-10-16
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 11 JDK 12
11.0.3-oracleFixed 12 b17Fixed
Description
A DESCRIPTION OF THE PROBLEM :
In case I create a JSpinner add a PropertyChangeListener and then call setFont(null) it results in a NullPointerException. It looks like the bug was introduced in Java 9 with the fix:
https://bugs.openjdk.java.net/browse/JDK-5036022
Instead of passing the font it has been wrapped in a FontUIResource first, but the FontUIResource does not allow null font. 
 tf.setFont(new FontUIResource(spinner.getFont())); 
The line should be:
 tf.setFont(spinner. getFont() == null ? null : new FontUIResource(spinner.getFont()));

REGRESSION : Last worked in version 8u172

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JSpinner
Add a PropertyChangeListener
Call setFont(null)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
NullPointerException should not be thrown
ACTUAL -
Exception in thread "main" java.lang.NullPointerException
	at java.desktop/java.awt.Font.<init>(Font.java:712)
	at java.desktop/javax.swing.plaf.FontUIResource.<init>(FontUIResource.java:68)
	at java.desktop/javax.swing.plaf.basic.BasicSpinnerUI$Handler.propertyChange(BasicSpinnerUI.java:1020)
	at java.desktop/java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:341)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:266)
	at java.desktop/java.awt.Component.firePropertyChange(Component.java:8728)
	at java.desktop/java.awt.Component.setFont(Component.java:1946)
	at java.desktop/java.awt.Container.setFont(Container.java:1777)
	at java.desktop/javax.swing.JComponent.setFont(JComponent.java:2769)
	at JSpinnerTest.main(JSpinnerTest.java:14)

---------- BEGIN SOURCE ----------
import javax.swing.JSpinner;

public class JSpinnerTest {
	public static void main(String[] args) {
		JSpinner spinner = new JSpinner();
		spinner.addPropertyChangeListener(evt -> {});
		spinner.setFont(null);
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
We could avoid this bug by not calling the setFont with null, but unfortunately, it is in an external library, so we cannot change.

FREQUENCY : always



Comments
Fix Request This fixes regression introduced in 9, and faced by users in 11u (I see 11.0.4-oracle as well). Patch applies cleanly to 11u, passes new regression test. New regression test fails without the patch.
13-03-2019

Creating a JSpinner and adding a PropertyChangeListener followed by call to setFont(null) results in a NullPointerException. Checked this for JDK 8u181 and above and could confirm this as a regression in JDK 9 and above. To verify, run the attached test case with respective JDK versions. Results: 8u181: OK 9: Fail 11 ea b28: Fail 12 ea b11: Fail Run with JDK 9: ================== $ java JSpinnerTest Exception in thread "main" java.lang.NullPointerException at java.desktop/java.awt.Font.<init>(Font.java:711) at java.desktop/javax.swing.plaf.FontUIResource.<init>(FontUIResource.java:68) at java.desktop/javax.swing.plaf.basic.BasicSpinnerUI$Handler.propertyChange(BasicSpinnerUI.java:1020) at java.desktop/java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:341) at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333) at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:266) at java.desktop/java.awt.Component.firePropertyChange(Component.java:8678) at java.desktop/java.awt.Component.setFont(Component.java:1899) at java.desktop/java.awt.Container.setFont(Container.java:1773) at java.desktop/javax.swing.JComponent.setFont(JComponent.java:2770) at JSpinnerTest.main(JSpinnerTest.java:7)
14-09-2018