简体   繁体   English

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

[英]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.

相关问题 我的 java 编译器在命令提示符下处理我的程序,但是当我尝试运行该程序时,它给了我一个错误 - My java compiler works on my program in the command prompt but when i try to run the program it gives me an error 尝试运行程序时出现Ant错误 - Ant error when I try to run my program 当该方法单独运行时,我的代码会尝试工作,但是当我在测试程序时调用该方法时…出现捕获错误 - try of my code works when the method is run on its own, but when I call the method when testing my program… catch errors appears 每当我在 IntelliJ 中运行我的项目时,它都可以正常工作,但是每当我尝试将其作为 jar 运行时,它都会引发异常 - Whenever I run my project in IntelliJ it works a fine, however whenever I attempt to run it as a jar it throws an exception 生成时工作正常,运行时输出错误 - Works fine when I build it, outputs an error when I run it 当我尝试在 Java 中运行我的套接字程序时,为什么会出现错误? - Why am I getting an error when I try to run my socket program in Java? 当我尝试调试我的项目时,它不会抛出这样的文件错误 - When i try to debug my project it throws no such file error 当我尝试运行我的程序时,视图无法打开? - View Doesn't Open When I Try To Run My Program? 当我尝试在Android Studio上运行我的Android程序时出现NullPointerException - NullPointerException when I try to run my Android Program on Android Studio 当我尝试运行它时,为什么我的程序会冻结/崩溃? - Why does my program freeze/crash when i try to run it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM