gpt4 book ai didi

java - 当我尝试在 Linux 上运行我的程序时抛出错误(在 Windows 上运行良好)

转载 作者:行者123 更新时间:2023-12-01 21:58:35 26 4
gpt4 key购买 nike

我正在制作一个 swing 应用程序,它在 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)

最佳答案

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

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);
}

关于java - 当我尝试在 Linux 上运行我的程序时抛出错误(在 Windows 上运行良好),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com