簡體   English   中英

調用showOpenDialog()時出現JFileChooser異常

[英]JFileChooser Exception at calling showOpenDialog()

我有一個按鈕,如果單擊了它,則應打開一個文件選擇器...但是我總是會收到此NullPointerException。

我讀到的東西可能是因為我正在事件dis之外啟動對話框。 線程,但是此調用EventQueue.invokeLater仍會調用EDT,對嗎?

這是源(否則,如果在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>

嘗試使用SwingUtilities.invokeLater(Runnable r)進行測試。 如果它修復了NPE,這將是事件分發的問題。 如果不是,則showOpenDialog父級的參數為null會出現問題。

基於堆棧跟蹤,似乎您使用的外觀(Synth)可能有問題。 我會先嘗試其他LaF。 並且建議始終將父窗口(或任何父組件)傳遞給JFileChooser.showOpenDialog()。 否則,有時在某些系統和環境中可能會發生奇怪的事情(例如,對話框顯示在主窗口后面,不同的顯示器上,甚至在桌面區域之外)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM