gpt4 book ai didi

java - 我是 java 新手,尝试使用 JFrameb 添加图像,但我收到错误 Exception in thread "AWT-EventQueue-0"java.lang.NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:10 24 4
gpt4 key购买 nike

我使用 netbeans 来创建代码,因为我不明白如何添加图像,但每当我在 Jcreator 中运行它时,我都会收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at org.me.myimageapp.ImageDisplay.initComponents(ImageDisplay.java:38)
at org.me.myimageapp.ImageDisplay.<init>(ImageDisplay.java:21)
at org.me.myimageapp.ImageDisplay$1.run(ImageDisplay.java:68)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

这是我的代码:

    package org.me.myimageapp;


public class ImageDisplay extends javax.swing.JFrame {

/** Creates new form ImageDisplay */
public ImageDisplay() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp /resources/1406_animado.gif"))); // NOI18N
jLabel1.setText("jLabel1");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(110, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(102, 102, 102))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(jLabel1)
.addContainerGap(88, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new ImageDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration

}

最佳答案

您正在尝试加载不存在于以下位置的图像:

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/resources/1406_animado.gif"))); // NOI18N

由于 getClass().getResource( xyz ) 返回 null,因此您会得到该异常。

您可以将包含图像的文件夹(以“/org/me/”等开头的结构)添加到类路径中,这样就可以了。

关于java - 我是 java 新手,尝试使用 JFrameb 添加图像,但我收到错误 Exception in thread "AWT-EventQueue-0"java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6365692/

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