Graphics throwing NullPointerException when running drawImage()
I've tried a few too many ways to repeatedly draw an image, and I know that using Graphics is better than initializing 500+ JLabels just to draw 1x100 images as a toolbar.
This code keeps throwing NullPointerExceptions and I simply can't figure out why. Maybe someone a bit more insightful than myself knows what I did wrong?
privatevoid addGUIComponents(JFrame jf){
JPanel btmbar =new JPanel();
btmbar.setBounds(0, jf.getHeight() - 100, jf.getWidth(), 150);
btmbar.setLayout(null);
//add the toolbar
for (int i = 0; i < jf.getWidth(); i++){
try{
BufferedImage img = ImageIO.read(new File("images/btm-bar.png"));// 1x100 image
Graphics g = btmbar.getGraphics();
g.drawImage(img,i, i, btmbar);
}catch (Exception e){
e.printStackTrace();
}
}
jf.getContentPane().add(btmbar);
}
That code throws the following for each time I try drawing the image:
java.lang.NullPointerException
at eyecandy1.gui.addGUIComponents(gui.java:67)
at eyecandy1.gui.<init>(gui.java:49)
at eyecandy1.gui.main(gui.java:77)

