I'm making a JPanel for a game. I want the background to be an imported image, so I set up a ClassLoader to import the image and converted that to a URL. However, that URL appears to be null, as when I try to put the panel into a JFrame, no image is drawn and a NullPointerException is thrown. Now I have an if statement to avoid that in the future, but I am mystified as to why the URL would be null in the first place. Any help would be appreciated!

import java.awt.*;
import java.net.URL;

public class StartPanel extends JPanel
{
    private ClassLoader loader=StartPanel.class.getClassLoader();

    private Toolkit tools=Toolkit.getDefaultToolkit();

    private URL startBackground=loader.getResource("Sword.PNG");

    public StartPanel()
    {
        
    }
    @ Override public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(startBackground!=null)
        {
            Image startBackgroundi=tools.createImage(startBackground);
            g.drawImage(startBackgroundi, 0, 0, 20, 30, this);
        }
    }
}

Is the image file on the classpath?

It is in the source package (I'm using Netbeans), so it's in the same directory as my java files... does it need to be where the class files are?

Sorry, I don't use Netbeans.

Put it all in a jar file and see it that works.

Running the jar file didn't work...

didn't work

Errors?

What is in the jar file? Did you put the images in there also?
Are there any error messages if you execute the jar file in a command prompt window?
If so, copy and paste them here.

No error messages and everything's in the jar... why would running it as a jar be any different than running the project as normal?

So when I tried running it, it gave me only a blank JFrame w/o the drawing

why would running it as a jar be any different than running the project as normal?

There wouldn't be an IDE involved.

Add some debugging code to your program to test if the image is loaded OK. For example:
println("image1=" + startBackground);
and
println("image2=" + startBackgroundi);
Then run it from the jar and see what is printed on the console.

Well, I did that and here's the output:

image1=null
image2=sun.awt.image.ToolkitImage@1cf8583

Should the image1 line be printing the URL of the image ("Sword.png") b/c startBackground is a URL?

It looks like your program is not finding the image file. The getResource() method is returning null.
Where is the image file its trying to find?

The article looks good, but I'm not at a level that I can understand most of it yet...

But I'm not sure why the URL would be null in the first place; the image is in the same folder as this file.

the image is in the same folder as this file.

What file is "this file"?

have you tried executing the program from a command prompt window?

Open a window and change directory to get to the folder with the class files and the image file. Enter the command:
java NameOfClassWithMainMethod

Maybe I gleaned more from that article than I thought... When I remove my ClassLoader from the equation and instead attempt to load the image directly using the class itself,

private URL startBackground=StartPanel.class.getResource("Sword.PNG");

the image is drawn and the test output is changed to the file location.

I appreciate the assistance both of you gave me here. I would also appreciate any ideas as to why the ClassLoader didn't work but the Class did. Thanks!

Okay, maybe I spoke too soon...

Today I tried it with a different image (but in the same directory) and it did not draw the image. The program, however, continues outputting results for as long as it is running. (It does the same with the original image, though the image is still drawn.) image1 (the URL) is shown as the location of the image file (as expected), but image2 (the Image created by the toolkit) is shown as a different ToolkitImage every time it is output; for instance, sun.awt.image.ToolkitImage@1e0cf70 changes to sun.awt.image.ToolkitImage@13f3789, etc. I have no idea what is going on here. Any ideas?

And in case it helps,

this file

is the subclass of JPanel I wrote in which I attempt to import and draw the image. There is also a Main class that creates a JFrame and sets the content pane to the JPanel extension

Don't know why but getImage() works for me vs using createImage()

Yeah, that works for me too, thanks. But it's still doing the weird thing with the output and changing the ToolkitImage number. Not that it really matters, but I'd be interested to know why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.