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 :-)