Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE on FlatHTML when the component has a null font #930

Closed
eduhoribe opened this issue Dec 6, 2024 · 1 comment · Fixed by #931
Closed

NPE on FlatHTML when the component has a null font #930

eduhoribe opened this issue Dec 6, 2024 · 1 comment · Fixed by #931
Milestone

Comments

@eduhoribe
Copy link
Contributor

After the addition of FlatHTML (261d2b1), setting an HTML text on a component with a null font will throw a NullPointerException.

While this is probably a very unlikely scenario, since the vanilla java theme support components with null fonts and HTML text, it may be best for FlatLaf to also support it.

If not, this restriction should probably be explicit.

Sample code to reproduce:

import com.formdev.flatlaf.FlatLightLaf;

import javax.swing.*;
import java.awt.*;

class Scratch {
    public static void main(String[] args) {

        FlatLightLaf.setup(); // commenting this line, no NPE is thrown with the label font being null

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new BorderLayout(10, 10));
            JLabel label = new JLabel();
            label.setFont(null); // commenting this line, no NPE is thrown with FlatLaf theme being active
            label.setText("<html><h1>foo</h1><h2>bar</h2></html>");
            frame.getContentPane().add(label, BorderLayout.CENTER);
            frame.setSize(new Dimension(100, 100));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

Stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.awt.Font.getSize()" because "font" is null
	at com.formdev.flatlaf.ui.FlatHTML.updateRendererCSSFontBaseSize(FlatHTML.java:75)
	at com.formdev.flatlaf.ui.FlatHTML.propertyChange(FlatHTML.java:243)
	at com.formdev.flatlaf.ui.FlatLabelUI.propertyChange(FlatLabelUI.java:131)
	at java.desktop/java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:343)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:335)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:268)
	at java.desktop/java.awt.Component.firePropertyChange(Component.java:8717)
	at java.desktop/javax.swing.JComponent.putClientProperty(JComponent.java:4180)
	at java.desktop/javax.swing.plaf.basic.BasicHTML.updateRenderer(BasicHTML.java:232)
	at java.desktop/javax.swing.plaf.basic.BasicLabelUI.propertyChange(BasicLabelUI.java:493)
	at com.formdev.flatlaf.ui.FlatLabelUI.propertyChange(FlatLabelUI.java:130)
	at java.desktop/java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:343)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:335)
	at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:268)
	at java.desktop/java.awt.Component.firePropertyChange(Component.java:8717)
	at java.desktop/javax.swing.JLabel.setText(JLabel.java:341)
	at Scratch.lambda$main$0(scratch_12.java:17)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

@DevCharly
Copy link
Collaborator

Thanks for reporting and the PR 👍 😄

Fixed in latest 3.6-SNAPSHOT: https://github.com/JFormDesigner/FlatLaf#snapshots

@DevCharly DevCharly modified the milestones: 3.6, 3.5.4 Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants