1

我是 Java 的新手,我正在尝试在我的窗口中添加一个按钮,但我遇到了一些麻烦。

首先,我创建了一个新的 jLabel,将“文本”留空,然后从我的图像文件夹(在我的项目中)中选择一个图像。之后,我添加了一个新的 jButton,再次将“文本”留空,并从我的图像文件夹中选择了一个图像。

这是我的项目结构:

control
Images
|- A lot of images (fireworks png)

Model
|- Connection.java

View
|- MainMenu.java

我认为一切正常,但是当我尝试运行此代码时,出现以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at view.TelaInicialLocus.initComponents(MainMenu.java:53)
    at view.TelaInicialLocus.<init>(MainMenu.java:17)
    at view.TelaInicialLocus$2.run(MainMenu.java:133)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

代码行:

line 17:
initComponents();

line 53/57:
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/logo2.png"))); // NOI18N
        jLabel3.setMaximumSize(new java.awt.Dimension(200, 200));
        jLabel3.setMinimumSize(new java.awt.Dimension(200, 200));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/entrar.fw.png")));

line 115/145:
public static void main(String args[]) {
        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(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaInicialLocus().setVisible(true);
            }
        });
    }

我愿意帮助提供更多信息和代码。我能做些什么来解决这个问题?

4

2 回答 2

2

我在这个问题中找到了一个很好的答案:为什么我的图标处理代码会抛出 NullPointerException?

我只需右键单击我的项目并选择“清理并构建”。

于 2013-05-26T04:14:44.910 回答
0

我之前遇到过这个问题......

使用 getClass().getResource("images/logo2.png") ...只需删除多余的 '/'

那个额外的斜线导致了问题......

于 2013-05-26T04:22:04.380 回答