user3552740
user3552740

Reputation: 9

Why can't I load Images?

I am making a game & trying to add a play button, but it says "The local variable playbutton may not have been initialized,". Here is my code:

class graphics extends JPanel{

ImageIcon playbutton = new ImageIcon("res\\playbutton");

public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    Image playbutton;
    g.drawImage(playbutton, 250, 300, null);
}

}

Upvotes: 0

Views: 43

Answers (1)

Ken White
Ken White

Reputation: 125620

Because the copmpiler is almost always right. In this case, your local declaration of Image playbutton is never initialized. It's a different variable than the one you have before the method, and declaring it locally hides the first (global) one of the same name.

Upvotes: 1

Related Questions