Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8210739

Calling JSpinner's setFont with null throws NullPointerException

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • Resolved In Build:
      b17
    • CPU:
      x86_64
    • OS:
      windows_10

      Backports

        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


          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  serb Sergey Bylokhov
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  4 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: