Java “线程中的异常”;AWT-EventQueue-0“;空指针异常
我是一个Java新手,我正试图在我的窗口中添加一个按钮,但我遇到了一些问题 首先,我创建了一个新的jLabel,将“文本”留空,并从我的images文件夹(在我的项目中)中选择了一个图像。之后,我添加了一个新的jButton,再次将“文本”留空,并从images文件夹中选择了一个imagem 以下是我的项目结构: 我认为一切正常,但当我尝试运行此代码时,出现以下错误消息:Java “线程中的异常”;AWT-EventQueue-0“;空指针异常,java,swing,user-interface,nullpointerexception,Java,Swing,User Interface,Nullpointerexception,我是一个Java新手,我正试图在我的窗口中添加一个按钮,但我遇到了一些问题 首先,我创建了一个新的jLabel,将“文本”留空,并从我的images文件夹(在我的项目中)中选择了一个图像。之后,我添加了一个新的jButton,再次将“文本”留空,并从images文件夹中选择了一个imagem 以下是我的项目结构: 我认为一切正常,但当我尝试运行此代码时,出现以下错误消息: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerEx
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)
线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常
位于javax.swing.ImageIcon.(ImageIcon.java:205)
在view.TelaInicialLocus.initComponents(main menu.java:53)
在view.TelaInicialLocus(MainMenu.java:17)
at view.TelaInicialLocus$2.run(main menu.java:133)
在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)中
位于java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
在java.awt.EventQueue.access$200(EventQueue.java:103)
在java.awt.EventQueue$3.run(EventQueue.java:688)
在java.awt.EventQueue$3.run(EventQueue.java:686)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
位于java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
在java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread.java:161)
位于java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThread.java:150)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
代码行:
第17行:
初始化组件();
第53/57行:
jLabel3.setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/images/logo2.png”);//NOI18N
jLabel3.setMaximumSize(新java.awt.Dimension(200200));
jLabel3.setMinimumSize(新java.awt.Dimension(200200));
setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/images/entra.fw.png”));
第115/145行:
公共静态void main(字符串参数[]){
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(TelaInicialLocus.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新的TelaInicialLocus().setVisible(true);
}
});
}
我愿意为您提供更多信息和代码。我能做些什么来解决这个问题 我在这个问题上找到了一个很好的答案:
我只需右键单击我的项目并选择“清理并构建”。我以前遇到过这个问题 使用getClass().getResource(“images/logo2.png”)…只需删除额外的“/”
这个额外的斜杠导致了问题…主菜单上的行是什么。java:53?您正在编写的代码是什么。?发布一些代码包…如果可能的话,将它们发布为poper行号eline 53:jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/images/logo2.png”));//NOI18N
getClass().getResource(“/images/logo2.png”)
正在返回null
。检查一下。
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);
}
});
}