NullPointerException when trying to use image reso

2019-02-20 13:38发布

I made a game in Java. It works completly fine in Eclipse.

I exported it as a Runnable JAR. When double-clicking on it's icon, it doesn't open. So I tried running the JAR from the command line.

I get a NullPointerException error, in a line of code that's trying to retrieve an image resource (as I said, it works fine inside Eclipse). This is the line of code where the error happens:

ball = new ImageIcon(this.getClass().getResource("sprites/ball.PNG"));

I have no idea what's wrong. Here is the structure of my project:

enter image description here

Any ideas? I'm starting to get desperate.

Thanks a lot for your help.

EDIT: I tried adding a / to the beginning of sprites/ball.PNG . Didn't help. Also tried to change PNG to png. Didn't work either.

Checked inside the JAR, the image is inside. I'm on Windows.

Here is the stacktrace:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init><Unknown Source>
    at instPanel.<init><instPanel.java:17>
    at Main.<init><Main.java:23>
    at Main.main<Main.java:38>

EDIT: Could the fact that I'm using (default package) be a problem?

4条回答
ら.Afraid
2楼-- · 2019-02-20 14:11

You load the URL with this.getClass().getResource("sprites/ball.PNG") but it returns: "A java.net.URL object or null if no resource with this name is found".

So the constructor from ImageIcon(URL location) gets null as value and if you look into the constructor you will see following line: this(location, location.toExternalForm());.

The problem lies in that exact line as location is null and therefor location.toExternalForm() throws your NPE.

You could fix it with

image = this.getClass().getResource("sprites/ball.PNG");
if (image != null) { 
    ball = new ImageIcon(image); 
}

but the underlying problem is that the image is not found.

What you can to to find (and fix) the error:

1 print somewhere the path in which it will be looking:

URL resource = this.getClass().getResource("/");
resource.getFile(); // print me somehwere

URL resource = this.getClass().getResource("img/");
resource.getFile(); // print me as well

URL resource = this.getClass().getResource("img/ball.PNG");
resource.getFile(); // print me as well

2 Ensure, that the image name / path matches the case exactly since it may be case sensitive.

查看更多
Juvenile、少年°
3楼-- · 2019-02-20 14:12

If you look inside the jar with an archive tool it should be like this.

JARROOT/*.class
JARROOT/sprites/ball.png

You can try following.

In Eclipse right click the sprites folder. Click Build Path -> Use as Source Folder and package the project again.

And your call to the resource should be this.getClass().getResource("/sprites/ball.png"); or this.getClass().getResource("/ball.png"); depending on how Eclipse packaged the jar.

EDIT: Please read the documentation.

If the string you pass to getResource("") begins with '/' it will be treated as an absolute path within the jar. If the string starts without '/' it will be treated as the relative path to the class.

Here is an image of a simple project with comments. The folder "icons" is a resource folder. Its content will be packaged inside the jar. In this case JARROOT/other/zoom-out-icon.JPG and JARROOT/zoom-in-icon.jpg.

enter image description here

查看更多
唯我独甜
4楼-- · 2019-02-20 14:34

I tried the code you gave and ran into the same problem with the uppercase PNG file. Later, when using 'ball.png', and using the below command:

java -classpath test.jar MainClass

I am able to access the image. What is the command you are using?

Output of jar -tf Test.jar:

META-INF/MANIFEST.MF
.classpath
Test.class
sprites/ball.png
.project
查看更多
贪生不怕死
5楼-- · 2019-02-20 14:38

Have you tried to call the getResource with /sprites/ball.PNG?

Other question: are you on linux system? If so, you should pay attention on uppercase (ball.PNG vs. ball.png).

EDIT:

BufferedImage image = ImageIO.read(getClass().getResource("/sprites/ball.PNG"));
ImageIcon icon = new ImageIcon(image);
查看更多
登录 后发表回答