0

当我尝试将图标添加到 Netbeans JFrame 中的按钮时,我遇到了一大堆错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at furnituremanagementsystem.menu.initComponents(menu.java:86)
at furnituremanagementsystem.menu.<init>(menu.java:17)
at furnituremanagementsystem.menu$12.run(menu.java:273)
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)

问题是它总是在我第二次尝试添加图标时发生。你们能解释一下这个错误是什么意思吗?谢谢你。PS 按钮代码:

jButton5 = new javax.swing.JButton();

jButton5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

jButton5.setForeground(new java.awt.Color(255, 0, 0));

jButton5.setIcon(new       javax.swing.ImageIcon(getClass().getResource("/furnituremanagementsystem/user-group-icon9878.png"))); // NOI18N

jButton5.setText("Customer");

jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton5ActionPerformed(evt);
}

});

 jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 170, 230, -1));
4

1 回答 1

0

如果您查看错误的第一行,它会显示NullPointerException. 这通常意味着您试图在空指针上调用方法。错误的其余部分是错误发生时刻的堆栈跟踪。这里看起来它发生在 ImageIcon 的第 205 行,这是一个构造函数ImageIcon,看起来像这样:

public ImageIcon (URL location) {
    this(location, location.toExternalForm()); // line 205
}

据我所知,您可能在furnituremanagementsystem.menu.initComponents()方法中将一个空 URL 对象传递给此构造函数。当这条线试图通过调用toExternalForm()它来使用它时,就会抛出一个空指针异常。

假设第 86 行是您正在构造 ImageIcon 对象的行,看起来似乎getResource()没有找到任何给定名称的东西,导致它返回 null。您应该尝试确定调用getResource()返回的是 null 还是 URL 对象。如果它返回 null,请找出它找不到该资源的原因。

仅供参考:这实际上只有 1 个错误。它看起来很多,因为你会得到一个堆栈跟踪。

于 2013-11-07T21:59:55.243 回答