-1

My goal right now is to set a background image with "black.png".

package testing;
import javax.swing.*;
import java.awt.*;
public class test extends Panel {
    test Panel;
    Image background;

    //image loading
    public void paint(Graphics bg) {
        bg.drawImage(background, 20, 20, null);
    }

    public static void main (String[] args) {
        Image background = Toolkit.getDefaultToolkit().createImage("black.png");
        test panel = new test();
        panel.paint(null);
    }
}

and the output :

Exception in thread "main" java.lang.NullPointerException
at testing.test.paint(test.java:10)
at testing.test.main(test.java:16)

Why doesn't line 10 and 16 point to null? And is it possible to just display a black background then overlay images that I want to?

Draken
  • 3,134
  • 13
  • 34
  • 54
xxboomxx
  • 11
  • 4

1 Answers1

0

You are explicitly passing null into your paint() method on line 16.

Kevin Swan
  • 147
  • 12