954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem importing images

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);
        }
    }
}
pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

Is the image file on the classpath?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

Sorry, I don't use Netbeans.

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Running the jar file didn't work...

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This smells like a path problem - it's often a bit trial-and-error to get the getResource parameter to match the actual jar folder hierarchy.
This article may help:
http://littletutorials.com/2008/03/26/locating-resources-in-java/

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 
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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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!

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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?

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: