I am developing a java project in eclipse. In order to distribute the program I have created a runnable .jar-file via the export-function in eclipse.
In my program a load several images that are stored in a folder called 'tableImages'. I load the images via the ClassLoader (you'll find the code snippets below). The problem is the following: When executing the program from the .jar-file, it throws a NullPointerException when loading one of the .png-files in the above mentioned folder. Now the funny thing is, that some of the .png-files in the exact same folder are loaded correctly, meaning they don't cause a NullPointerException.
I checked the content of the .jar using the jar tf command. The image that apparently cannot be loaded, was packed into the jar. So what is causing this NullPointerException and how can I solve it?
Here is the class with which I draw images to my swing-frame.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;
public class LoadBackgroundImage extends Component {
private static final long serialVersionUID = 1L;
BufferedImage img;
public void paint(Graphics g){
g.drawImage(img, 0, 0, null);
}
public LoadBackgroundImage(String image){
URL url = ClassLoader.getSystemResource(image);
try{
img = ImageIO.read(url);
this.setSize(img.getWidth(), img.getHeight());
}catch(IOException e){
System.err.println(e.getMessage());
}
}
public Dimension getPreferredSize(){
if(img == null){
return new Dimension(100, 100);
}else{
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public BufferedImage getImage(){
return img;
}
}
Thank you very much for your help!