- First and foremost, DON'T catch the
IllegalArgumentException
. This error suggests something very bad in your code, and shouldn't really be caught. See Jon Skeet's answer here for more on why this is bad. Instead you should: - Separate the code in your line where you read your image.
- First get the URL from the
getResource()
- Check if it's null
- If null don't call
ImageIO.read(...)
with it. - You should learn and follow Java naming conventions so that others will better understand your code. For instance, don't capitalize non-constant variables.
i.e.,
try{
URL imgUrl = getClass().getResource(IMAGE_PATH); // path should be a constant
// or variable, not a String literal
if (imgUrl == null) {
// show error and get the heck out of here
} else {
image = ImageIO.read(imgUrl);
}
} catch (IOException ex){
JOptionPane.showMessageDialog(null, "<html>Error<br>Missing images</html>" ,
"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}