线程“main”中的异常 java.lang.NullPointerException at javax.swing.ImageIcon.<init>(未知来源)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27999380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source)
提问by Jindra
I am working on a little program for school however I am getting this error whenever I run it:
我正在为学校开发一个小程序,但是每当我运行它时都会收到此错误:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Creatieve.Opracht.Main.main(Main.java:14)
The langugae I am coding in is Java, and before someone comments on it, no this is not a computer sience project so I can't ask a teacher.
我正在编码的语言是 Java,在有人评论它之前,不,这不是计算机科学项目,所以我不能问老师。
This is de code I have written:
这是我写的de代码:
package Creatieve.Opracht;
import java.awt.*;
import javax.swing.*;
public class Main {
static JFrame frame;
static Puzzel puzzel;
public static void main(String[] args){
frame = new JFrame("CKV Creatieve Oprdacht 2.0");
frame.setSize(900, 900); //Lengte en breedte van de foto
puzzel = new Puzzel(new ImageIcon(Main.class.getResource("/image.png")).getImage());
frame.add(puzzel);
frame.setVisible(true);
}
}
And also this:
还有这个:
package Creatieve.Opracht;
import java.awt.*;
import javax.swing.*;
public class Puzzel extends JPanel{
Onderdeel[] onderdelen;
Image img;
public Puzzel(Image img){
this.img = img;
onderdelen = new Onderdeel[9];
int onderdeelGrootte = img.getWidth(null)/3;
for(int i = 0; i != onderdelen.length; i++){
onderdelen[i] = new Onderdeel(this, i, onderdeelGrootte);
}
}
public void paint(Graphics g){
super.paint(g);
for (int i = 0; i != onderdelen.length; i++){
onderdelen[i].paint(g);
}
}
}
It would be awesome if one of you could tell me how I can fix the problem.
如果你们中的一个人能告诉我如何解决这个问题,那就太棒了。
Thank you in advance!
先感谢您!
回答by atish shimpi
Exception came on this line in your code when you are accessing image,new ImageIcon(Main.class.getResource("/image.png")).getImage()
.
当您访问图像时,代码中的这一行出现异常new ImageIcon(Main.class.getResource("/image.png")).getImage()
。
you have to check that /image.png
image available at this location or not, if possible use qualified path name of your folder to resolve issue.
您必须检查/image.png
该位置是否可用该图像,如果可能,请使用文件夹的限定路径名来解决问题。
回答by Jyoti Prakash Sahoo
The resource file /image.png
does not exists. So Main.class.getResource("/image.png")
is returning null. Hence while creating the ImageIcon object with null
is resulting in NullPointerException
资源文件/image.png
不存在。Main.class.getResource("/image.png")
返回空值也是如此。因此,在创建 ImageIcon 对象时null
会导致NullPointerException
回答by Sandeep kadam
to solve this issue..... just copy your images to /your_Drive_in_which_workspace_folder_is_present/workspace/projectname/bin/projectname/
解决这个问题..... 只需将您的图像复制到 /your_Drive_in_which_workspace_folder_is_present/workspace/projectname/bin/projectname/
now this statement should be like this:
现在这个语句应该是这样的:
Icon IconObject=new ImageIcon(getClass().getResource("Image_name.png"));
Icon IconObject=new ImageIcon(getClass().getResource("Image_name.png"));