0

我有一个按钮,如果单击它,应该打开一个文件选择器......但我总是得到这个 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>
4

2 回答 2

0

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

于 2012-09-18T13:50:25.437 回答
0

根据stacktrace,您使用的外观(合成器)似乎有问题。我会先尝试不同的 LaF。并且建议始终将父窗口(或任何父组件)传递给 JFileChooser.showOpenDialog()。否则在某些系统和环境中有时可能会发生奇怪的事情(例如,对话框显示在主窗口的后面、不同的显示器上,甚至在桌面区域之外)。

于 2012-09-18T13:50:26.153 回答