Passage
Passage

Reputation: 234

Java logic error that doesn't always exist

I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's some sort of bizarre logic error in my paintComponent method, which is preventing images from being drawn properly. Normally this wouldn't be an issue, and I could find a way around it. However, the problem doesn't always exist. Commenting and decommenting one line at a time will sometimes get around it, as will closing and reopening the IDE. (Eclipse.) What could possibly cause this, and what can be done to avoid it?

Here's my method:

public void paintComponent(Graphics g){
    g.drawImage(titlebg, 0, 0, null);
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(300, 250, 300, 50);
    //g.fillRect(400, 400, 300, 50);
    g.drawImage(ccground, 0, 0, null);
}

EDIT: The drawImage methods are not functioning when the setColor and fillRect methods are uncommented. Sometimes they don't work even if it's just the two of them. Sometimes they do.

EDIT 2: The only other call to the images is in the following code, which initializes them.

Toolkit tk = Toolkit.getDefaultToolkit();

public Image retrieveImage(String x){
    URL y = this.getClass().getResource(x);
    Image img = tk.getImage(y);
    return img;
}

This is referenced with:

public void loadImages(){
    ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
    titlebg = rl.retrieveImage("\\Title Screen.png");
}

in the same class as the paintComponent method.

EDIT 3: Stacktrace.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at gui.Main.createGui(Main.java:32)
at gui.Main$1.run(Main.java:21)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Main.class

public class Main extends JFrame{
static Window w;
JFrame f;
Image ccground;
Main(){
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            createGui();
        }
    });

}
public static void main(String[] args){
    w = new Window();
}
public void createGui(){
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(w);
    f.pack();
    f.setVisible(true);
    f.setBackground(Color.BLACK);
}
}

EDIT 4: Window.java

public class Window extends JPanel{
Main m;
ResourceLoader rl;
Image ccground;
Image titlebg;
public Window(){
    rl = new ResourceLoader();
    m = new Main();
    loadImages();
}
public Dimension getPreferredSize(){
    return new Dimension(800, 592);
}
public void loadImages(){
    ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
    titlebg = rl.retrieveImage("\\Title Screen.png");
}
public void paintComponent(Graphics g){
    g.drawImage(titlebg, 0, 0, null);
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(300, 250, 300, 50);
    //g.fillRect(400, 400, 300, 50);
    g.drawImage(ccground, 0, 0, null);
}
}

Upvotes: 0

Views: 260

Answers (1)

Durandal
Durandal

Reputation: 20059

Toolkit.getDefaultToolkit().getImage(url) is the culprit. The method gets you an image that is not necessarily loaded yet (an empty dummy in a way). Since you don't do anything to ensure the image is loaded, it sometimes is and sometimes it isn't.

One of many ways to ensure the image is completely loaded before using it is (shortest one I know of) to excute this for each image obtained from Toolkit before using it:

new ImageIcon(image).getImage();


EDIT:

Alter you retrieveImage like this and it should work OR clearly indicate the image was not found:

public Image retrieveImage(String x){
    URL y = this.getClass().getResource(x);
    Image img = tk.getImage(y);
    if (img == null)
        throw new IllegalArgumentException("image " + x + " not found");
    return new ImageIcon(img).getImage();
}

Upvotes: 2

Related Questions