Java:在MetalFileChooserUI $ IndentIcon.getIconWidth中防止NPE吗?

时间:2020-03-06 14:25:06  来源:igfitidea点击:

在Windows系统上。我得到以下带有FileChooser的NPE。这是一个尚不被太阳修复的已知错误。 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.access0(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)

解决方案

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

A DESCRIPTION OF THE PROBLEM :
  There appears to be an undocumented bad intereaction between
  explicitely setting the UI and removing all file filters, even temporarily.
  If the latter is done before setting the ui, trying to display a file dialog
  will throw an exception but not if the ui was set prior to messing with
  the filters. Maybe it is possible to make the code more robust against
  this or to include a warning in the docs?
  
  STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
  Run the attached program: it will not bomb. Then move the setUI
  line to the bottom of the constructor and try again: it will.

因此,现在使用注册帐户:)

提到的链接中这些步骤的问题在于,在我们的软件中全局设置了外观和UI。因此,在我能够操作文件过滤器之前就已经设置了UI。

编辑:误解了要复制的代码。所提到的示例作品。谢谢。

变通办法说明似乎表明我们应在操作过滤器之前尝试设置UI。这行不通吗?

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