SwingWorker中抛出异常

时间:2012-10-17 13:35:37

标签: java swing swingworker

我尝试在SwingWorker类的importData(..)方法中使用TransferHandler(以支持拖放)。我想显示我拖动的文件的导入进度。但是,它不起作用。我不明白问题是什么,或者是什么导致它。

private class FileImportTransferHandler extends TransferHandler {

    @Override
    public boolean importData(JComponent comp, final Transferable t) {

        SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {

            @Override
            protected Void doInBackground() throws Exception {
                //When commenting this code it and everything else it doesn't
                DataFlavor[] flavors = t.getTransferDataFlavors(); //<---- throws exception 
                //..more code that doesn't run.
            }

            @Override
            protected void done() {
                super.done();

                try {
                    get();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ImportBeanController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ExecutionException ex) {
                    Logger.getLogger(ImportBeanController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
       //Starting worker etc here.
    }       
}

例外:

  

java.util.concurrent.ExecutionException:   java.lang.NullPointerException at   java.util.concurrent.FutureTask $ Sync.innerGet(FutureTask.java:222)at at   java.util.concurrent.FutureTask.get(FutureTask.java:83)at   javax.swing.SwingWorker.get(SwingWorker.java:582)at   com.jatech.app.controllers $ ImportBeanController $ 1.done(ImportBeanController.java:158)     在javax.swing.SwingWorker $ 5.run(SwingWorker.java:717)at   javax.swing.SwingWorker $ DoSubmitAccumulativeRunnable.run(SwingWorker.java:814)     at sun.swing.AccumulativeRunnable.run(AccumulativeRunnable.java:95)     在   javax.swing.SwingWorker $ DoSubmitAccumulativeRunnable.actionPerformed(SwingWorker.java:824)     在javax.swing.Timer.fireActionPerformed(Timer.java:291)at   javax.swing.Timer $ DoPostEvent.run(Timer.java:221)at   java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)at   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)at at   java.awt.EventQueue.access $ 000(EventQueue.java:84)at   java.awt.EventQueue $ 1.run(EventQueue.java:602)at   java.awt.EventQueue $ 1.run(EventQueue.java:600)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:87)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:611)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)   引起:java.lang.NullPointerException at   sun.awt.dnd.SunDropTargetContextPeer.getTransferDataFlavors(SunDropTargetContextPeer.java:176)     在   sun.awt.datatransfer.TransferableProxy.getTransferDataFlavors(TransferableProxy.java:48)     在   java.awt.dnd.DropTargetContext中$ TransferableProxy.getTransferDataFlavors(DropTargetContext.java:327)

1 个答案:

答案 0 :(得分:3)