0

I'm currently loading my images using the following function:

public static Image loadImage(String path){
        log.debug("loadImage> path: "+path);
        return Toolkit.getDefaultToolkit().getImage(ClientApp.class.getClassLoader().getResource(path));
    }

Utils.loadImage("images/carrot_stage_0.png")

This function throws a NullPointerException when i package my jar and run it from the command line:

          - loadImage> path: images/carrot_stage_0.png
java.lang.NullPointerException
        at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
        at sun.awt.SunToolkit.getImage(Unknown Source)
        at com.tate.world.client.Utils.loadImage(Utils.java:11)
        at com.tate.world.client.Carrot.<init>(Carrot.java:24)
        at com.tate.world.client.Inventory.<init>(Inventory.java:32)
        at com.tate.world.client.DrawPanel.<init>(DrawPanel.java:35)
        at com.tate.world.client.ClientFrame.doInit(ClientFrame.java:46)
        at com.tate.world.client.ClientApp$1.run(ClientApp.java:18)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.doIntersectionP
rivilege(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)

But it doesn't throw the same exception when i run it from my IDE eclipse.

It first didn't make any sense and i thought my code was wrong, after trying many variations i checked if the image was available. It turned out the filename was wrong, i used "carrot_stage_0.png" but in my classpath it was names "carrots_stage_0.png". After i corrected it it started working.

But this raises the question why does the following code doesn't throw any kind of exception when the image, that is being loaded, can't be loaded (in eclipse). But it does throw an NullPointerException on the command line.

Toolkit.getDefaultToolkit().getImage(ClientApp.class.getClassLoader().getResource(path)

(same java 8 versions are used)

It puzzles me :-)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tinus Tate
  • 2,237
  • 2
  • 12
  • 33
  • Where is the image stored? – MadProgrammer Oct 10 '15 at 07:45
  • @MadProgrammer on the classpath in /images/ (/src/main/java/images/) . In the jar it is also in /images/ (world2d.jar\images\ ) , which is a simple executable jar (used export function in eclipse) – Tinus Tate Oct 10 '15 at 08:31
  • 1
    Have you tried using the path `/images/carrot_stage_0.png`? And wouldn't bother using `ToolKit` to load your images and instead use `ImageIO.read`, it supports more formats, throws a `IOException` when the image can't be loaded and only returns once the image has been fully loaded rather then off loading it to a background thread some where – MadProgrammer Oct 10 '15 at 08:52
  • @MadProgrammer adding the root slash does work in eclipse, but not on command line, that's why i removed it, without it it works in both cases. But that's not really the problem, i was wondering about the behavior that an exception is thrown on command line but not in eclipse while executing the same code with the same classpath, that does seem inconsistent. It is not world shocking thing, it just puzzles me a bit. On a side note I'm going to give ImageIO.read a shot! – Tinus Tate Oct 10 '15 at 09:09
  • The only reason `getResource` would return `null` is because it can't find the named resource. I assume you've cracked the jar open and checked it's contents – MadProgrammer Oct 10 '15 at 09:34
  • @MadProgrammer, yes i did, but i suspect, because you ask this question, my question is a bit confusing to you. I expect an exception when the image is loaded, and this image is not found (so no image is available). But in eclipse i don't get such an exception, on the commandline i get one. So i expect a exception, but i don't get one ;-) – Tinus Tate Oct 10 '15 at 09:47

0 Answers0