我有一个按钮,如果单击它,应该打开一个文件选择器......但我总是得到这个 NullPointerException。
我读到了一些可能是因为我在事件 dis 之外启动对话框。线程,但是这个调用 EventQueue.invokeLater 无论如何都会调用 EDT,对吧?
这是源代码(else if 在 ActionListener 扩展类中 - 在 actionPerformed 方法中):
else if(buttonID.equals(Definition.BTN_LOAD_DF_NAME))
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter()
{
@Override
public String getDescription(){
return "XML-File";
}
@Override
public boolean accept(File f){
return f.isDirectory() || f.getName().endsWith(".xml") || f.getName().endsWith(".XML");
}
});
int state = fc.showOpenDialog(null);
if(state == JFileChooser.APPROVE_OPTION)
{
try{
app.loadDF(fc.getSelectedFile());
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
});
}
这里是堆栈跟踪:
Exception occurred during event dispatching:
java.lang.NullPointerException
at sun.swing.plaf.synth.SynthFileChooserUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.Dialog$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Unknown Source)
at javax.swing.JFileChooser.showDialog(Unknown Source)
at javax.swing.JFileChooser.showOpenDialog(Unknown Source)
at logic.FuncActionListener$1.run(FuncActionListener.java:120)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
感谢您的每一个帮助!
ps:观感(必用):
<?xml version="1.0" encoding="UTF-8"?>
<synth>
<style id="backingStyle">
<opaque value="FALSE" />
</style>
<bind style="backingStyle" type="region" key="Panel" />
<style id="button">
<!-- Shift the text one pixel when pressed -->
<property key="Button.textShiftOffset" type="integer" value="2" />
<state>
<imagePainter method="buttonBackground" path="images/button_normal.png" sourceInsets="10 10 10 10" />
<font name="Dialog" size="12" />
</state>
<state value="PRESSED">
<imagePainter method="buttonBackground" path="images/button_normal_pressed.png" sourceInsets="10 10 10 10" />
</state>
</style>
<bind style="button" type="region" key="Button" />
<style id="wndBG">
<imagePainter methode="wndBackground" path="images/cdu_bg.png" sourceInsets="1 1 1 1"></imagePainter>
</style>
<bind style="wndBG" type="name" key="root"></bind>
</synth>