我在这个示例的最后一行调用drawImage()时得到一个NullPointerException,即使我没有给它一个null变量。帮助将不胜感激。
BufferedImage image = null;
System.out.println(image);
try {
image = ImageIO.read(getClass().getResource("/gameobjects/diaz.jpg"));
System.out.println("eter");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(image);
Graphics g = mainPanel.getGraphics();
g.drawImage(image, 50, 50, null);
和堆栈跟踪:
null
eter
BufferedImage@6a5f6303: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@1c5d9084 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 384 height = 216 #numDataElements 3 dataOff[0] = 2
Exception in thread "main" java.lang.NullPointerException
at Main.<init>(Main.java:41)
at Main.main(Main.java:17)
答案 0 :(得分:-3)
我认为您需要将"/gameobjects/diaz.jpg"
更改为"gameobjects/diaz.jpg'
而不使用前导&#39; /&#39;。如果是这样的话,因为与绝对资源路径相比,它具有相对资源路径。 &#39; /&#39;指定绝对,所以它正在寻找C:/ game ...(在Windows上)而不是/ game ...