Wednesday, 21 August 2013

keep getting the nullpointerException? i am trying to print out an image

keep getting the nullpointerException? i am trying to print out an image

here is the main class
public class testing extends JFrame{
private static final long serialVersionUID = 1L;
public testing(){
setContentPane(new Canvas());
setSize(800,600);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args0){
new testing();
}
}
and the error occurs in the Canvas class, the drawImage method. I've made
a res folder where i put my images and use it as source folder.
public class Canvas extends JPanel{
BufferedImage image;
Graphics2D g;
private static final long serialVersionUID = 1L;
public Canvas(){
setPreferredSize(new Dimension(800,600));
loadImage("/space.png");
PaintComponents(g);
}
public void PaintComponents(Graphics2D g){
g.drawImage(image, 0,0,null);
}
public void loadImage(String path){
try {
image = ImageIO.read(
getClass().getResourceAsStream(path)
);
}
catch(Exception e) {
e.printStackTrace();
System.out.println("image loading error");
}
}
}
Thank you for the help.

No comments:

Post a Comment