[英]My program throws an error when I try to run it on Linux (works fine on windows)
I'm making a swing aplication, which works "fine" on windows, but it sends an error on the (*) line. 我正在做一个摆动应用程序,在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
});
}
I already tried changing the code to what netbeans suggested, and it keeps showing the same error. 我已经尝试将代码更改为netbeans所建议的内容,并且一直显示相同的错误。
This is what netbeans changes: 这就是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
}
});
Here is 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)
Why would you ever want to set the dialog to visible if creating the dialog has failed? 如果创建对话框失败,为什么还要将对话框设置为可见?
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);
}
Additionally 另外
You should use imports ( import java.util.logging.*
) in a way that stops you having to write extremely ugly lines like 您应该以某种方式使用import( 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);
}
and simplify them to 并简化为
} 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.