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

Null Pointer Exception on Windows shortcut panel #718

Closed
ptorngren opened this issue Aug 22, 2023 · 2 comments
Closed

Null Pointer Exception on Windows shortcut panel #718

ptorngren opened this issue Aug 22, 2023 · 2 comments
Milestone

Comments

@ptorngren
Copy link

ptorngren commented Aug 22, 2023

One of our users reported an error which seems to be caused by the FlatShortcutsPanel constructor not accounting for a null name, which javax.swing.filechooser.FileSystemView.getSystemDisplayName() may return.

We get the following stack trace in FlatLaf 3.0:

java.lang.NullPointerException: Cannot invoke "String.lastIndexOf(int)" because "name" is null
   at com.formdev.flatlaf.ui.FlatFileChooserUI$FlatShortcutsPanel.<init>(FlatFileChooserUI.java:444)
   at com.formdev.flatlaf.ui.FlatFileChooserUI.createShortcutsPanel(FlatFileChooserUI.java:318)
   at com.formdev.flatlaf.ui.FlatFileChooserUI.installComponents(FlatFileChooserUI.java:181)
   at java.desktop/javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:209)
   at java.desktop/javax.swing.plaf.metal.MetalFileChooserUI.installUI(MetalFileChooserUI.java:159)
   at java.desktop/javax.swing.JComponent.setUI(JComponent.java:730)
   at java.desktop/javax.swing.JFileChooser.updateUI(JFileChooser.java:1839)
   at com.onseven.dbvis.util.gui.r.b.updateUI(Z:320)
   at java.desktop/javax.swing.JFileChooser.setup(JFileChooser.java:396)
   at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:362)
   at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:349)
@DevCharly
Copy link
Collaborator

Was not able to reproduce the NPE...

Anyway, improved the implementation and ignore items where file or name are null.

If you need a quick fix for FlatLaf 3.0, you could use following:

public class MyFlatFileChooserUI extends FlatFileChooserUI {
    public static ComponentUI createUI( JComponent c ) {
        return new MyFlatFileChooserUI( (JFileChooser) c );
    }

    public MyFlatFileChooserUI( JFileChooser filechooser ) {
        super( filechooser );
    }

    @Override
    protected FlatShortcutsPanel createShortcutsPanel( JFileChooser fc ) {
        return new MyFlatShortcutsPanel( fc );
    }

    public static class MyFlatShortcutsPanel extends FlatShortcutsPanel {
        public MyFlatShortcutsPanel( JFileChooser fc ) {
            super( fc );
        }

        @Override
        protected String getDisplayName( FileSystemView fsv, File file ) {
            String name = super.getDisplayName( fsv, file );
            return name != null ? name : "???";
        }
    }
}

To enable above class use:

if( UIManager.getLookAndFeel() instanceof FlatLaf )
    UIManager.put( "FileChooserUI", MyFlatFileChooserUI.class.getName() );
else
    UIManager.put( "FileChooserUI", null );

@DevCharly DevCharly added this to the 3.3 milestone Aug 24, 2023
@DevCharly
Copy link
Collaborator

fixed in FlatLaf 3.2.1

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

No branches or pull requests

2 participants