I'm getting a NullPointerException when calling drawImage() on the last line of this sample even though I'm not giving it a null variable. Help would be appreciated.
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);
and the stack trace:
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)