Java:如何防止MetalFileChooserUI$INDIDIcon.getIconWidth中的NPE?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (139)

在Windows系统上得到了以下NPE FileChooser。http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6342301

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException at javax.swing.plaf.metal.MetalFileChooserUI$IndentIcon.getIconWidth(Unknown Source) at javax.swing.SwingUtilities.layoutCompoundLabelImpl(Unknown Source) at javax.swing.SwingUtilities.layoutCompoundLabel(Unknown Source) at javax.swing.plaf.basic.BasicLabelUI.layoutCL(Unknown Source) at javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source) at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source) at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source) at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source) at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source) at javax.swing.JList.setSelectedIndex(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup.setListSelection(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup.access$300(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup$Handler.itemStateChanged(Unknown Source) at javax.swing.JComboBox.fireItemStateChanged(Unknown Source) at javax.swing.JComboBox.selectedItemChanged(Unknown Source) at javax.swing.JComboBox.contentsChanged(Unknown Source)

提问于
用户回答回答于

在链接到的错误报告中,他们还提到了一种解决方法。似乎归结为按特定顺序调用方法。你试过吗?

问题描述:在明确设置UI和删除所有文件过滤器之间,甚至是暂时的,似乎存在无证的不良互动。如果在设置ui之前完成后者,则尝试显示文件对话框将抛出异常,但如果在弄乱过滤器之前设置了ui则不会。也许有可能使代码更强大,或在文档中包含警告? 以下重现问题的步骤:运行附加程序:它不会炸弹。然后将setUI行移动到构造函数的底部,然后重试:它会。

用户回答回答于

看起来解决方法说明您应该在操作过滤器之前尝试设置UI。这不起作用吗?

如果这不起作用,是否可以在设置UI的同一点创建操纵的FileFilters实例?

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励