[英]nullpointerexception when declaring new ImageIcon
I am trying to load multiple images on the same window, so to prevent a lot of copy and paste, I made an image class called badgeIMG that looks like this: 我试图在同一个窗口加载多个图像,所以为了防止大量复制和粘贴,我创建了一个名为badgeIMG的图像类,如下所示:
package BattleSim;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class badgeIMG extends JPanel{
Image badgeIcon;
String badgePath;
int x = 0;
int y = 0;
public badgeIMG() {
ImageIcon ii = new ImageIcon(this.getClass().getClassLoader().getResource(badgePath));
badgeIcon = ii.getImage();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(badgeIcon, x, y, null);
}
}
Then in another class, called badgeSelectionWindow, I have this piece of code: 然后在另一个名为badgeSelectionWindow的类中,我有这段代码:
badgeIMG allOrNothingBadge = new badgeIMG();
badgeIMG closeCall = new badgeIMG();
allOrNothingBadge.badgePath = "/Badges/allornothing.gif";
allOrNothingBadge.x = 128;
allOrNothingBadge.y = 144;
closeCall.badgePath = "/Badges/closecall.gif";
closeCall.x = 256;
closeCall.y = 144;
add(allOrNothingBadge);
add(closeCall);
The problem is, I get a NullPointerException when declaring a badgePath from the above code, but when I put replace badgePath with one of the real file paths, it does not give me error, but I want to be able to plug in a String with the file path and have it display multiple images. 问题是,我从上面的代码声明一个badgePath时得到一个NullPointerException,但是当我把replace badge与其中一个真正的文件路径放在一起时,它不会给我错误,但是我希望能够插入一个String文件路径并让它显示多个图像。 Any ideas?
有任何想法吗?
Here is the error: 这是错误:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at BattleSim.badgeIMG.<init>(badgeIMG.java:17)
at BattleSim.badgeSelectionWindow.<init>(badgeSelectionWindow.java:11)
at BattleSim.badgeSelectionWindow.main(badgeSelectionWindow.java:36)
Your badgePath
is null
. 您的
badgePath
为null
。
The constructor uses the badgePath
as argument for the ImageIcon
constructor, but it didn't initialize it first. 构造函数使用
badgePath
作为ImageIcon
构造函数的参数,但它没有首先初始化它。 Use a constructor like this: 使用这样的构造函数:
public badgeIMG(String path)
{
ImageIcon ii = new ImageIcon(this.getClass().getClassLoader().getResource(path));
badgeIcon = ii.getImage();
badgePath = path;
}
Note : Very important: Java naming conventions are that classes start with an uppercase char. 注意 :非常重要:Java命名约定是类以大写字符开头。 So change the class name and file name to:
BadgeImg
or BadgeIMG
. 因此,将类名和文件名更改为:
BadgeImg
或BadgeIMG
。
this.getClass().getClassLoader().getResource(badgePath))
Your badgePath is null.
您的badgePath为
null.
Please initialize it with image path. 请用图像路径初始化它。
When you trying to have an image , you should give a path. 当你想要一个图像时,你应该给出一个路径。 Right now it is
null
. 现在它是
null
。 Giving a path in deceleration or assigning a value to it before using it resolves your issue. 在使用减速度路径或为其分配值之前,它可以解决您的问题。
String badgePath="some/valid/path";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.