我在NullPointerException
编译代码时遇到了问题,但它在 eclipse 中运行良好。
命令提示符的错误日志:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
获取图像的类是:
package tileGame;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
public class Image {
public String address1;
public String address2;
public String name;
public String type;
public java.awt.Image image;
public Image(String name){
address1 = System.getProperty("user.dir");
address2 = "/Resources/Images/";
type = "png";
this.name = name;
}
public Image(String name, String type){
address1 = System.getProperty("user.dir");
address2 = "/src/Resources/Images/";
this.type = type;
this.name = name;
}
public Image(String name, String type, String address){
address1 = System.getProperty("user.dir");
this.address2 = address;
this.type = type;
this.name = name;
}
public void loadImage(){
//image = new ImageIcon(address1 + address2 + name + "." + type).getImage();
image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(address2 + name + "." + type))).getImage();
System.out.println("Image Loaded '" + name + "' at '" + address2 + name + "." + type + "'");
}
}
这可能有一个很好的理由我看不到,因为我对 Java 还很陌生,所以如果有人能给我一些指示,那将是受欢迎的。
编辑:好的,我修复了我自己不会发现的常见人为错误。谢谢。