JFileChooser调用showOpenDialog()时的异常

时间:2012-09-18 13:23:53

标签: java swing exception jfilechooser

我有一个按钮,如果点击它,应该打开文件选择器... 但我总是得到这个NullPointerException。

我读了一些可能的内容,因为我在事件dis之外开始对话。线程,但这个调用EventQueue.invokeLater,无论如何调用EDT,对吗?

这是源(else if 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();
                        }
                    }                   
                }
            });
        }

这里是stacktrace:

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)

感谢您的帮助!!

p.s。:外观(必须使用):

<?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>

2 个答案:

答案 0 :(得分:0)

尝试使用SwingUtilities.invokeLater(Runnable r)来测试它是否有效。如果它修复了NPE,这将是事件调度的问题。如果不是,那么showOpenDialog父参数为null将是一个问题。

答案 1 :(得分:0)

基于堆栈跟踪,看起来你使用的外观和感觉可能有问题(Synth)。我会首先尝试不同的LaF。 建议始终将父窗口(或任何父组件)传递给JFileChooser.showOpenDialog()。否则在某些系统和环境中有时会发生奇怪的事情(例如,对话框显示在主窗口后面,不同的显示器上,甚至是桌面外的区域)。