当我尝试在Linux上运行它时,我的程序会抛出错误(在Windows上运行正常)

时间:2015-12-02 15:36:53

标签: java swing netbeans

我正在做一个摆动的应用程序,它可以正常运行"很好"在Windows上,但它会在(*)行上发送错误。

public static void main(String[] args) throws ParseException, java.lang.InstantiationException {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }




    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(() -> {
        MainView dialog = null;
        try {
            dialog = new MainView(new javax.swing.JFrame(), true);
        } catch (ParseException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
        }

        dialog.setVisible(true); /// (*) This line throws the error
    });
}

我已经尝试将代码更改为netbeans建议的内容,并且它会一直显示相同的错误。

这就是netbeans的变化:

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            MainView dialog = null;
            try {
                dialog = new MainView(new javax.swing.JFrame(), true);
            } catch (ParseException | SQLException | ClassNotFoundException ex) {
                Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
            }

            dialog.setVisible(true); /// (*) This line throws the error
        }
    });

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at view.MainView.lambda$main$0(MainView.java:2842)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:3)

如果创建对话框失败,为什么还要将对话框设置为可见?

try {
   dialog = new MainView(new javax.swing.JFrame(), true);
   dialog.setVisible(true); //  <--- move it here
} catch (ParseException | SQLException | ClassNotFoundException ex) {
   Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}

<强>另外

你应该使用导入(import java.util.logging.*)来阻止你写出非常难看的行,比如

} catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

并将它们简化为

} catch (ClassNotFoundException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}