We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › detect a missing resource file
Page Index Toggle Pages: 1
detect a missing resource file? (Read 396 times)
detect a missing resource file?
Dec 13th, 2007, 3:21am
 
So I know loadImage returns null if the image requested insn't there, but it still seems to crash my program when I test for null. How can I gracefully handle this situation?? (i.e. some kind of try catch, or some "isFileAvaialable()" function...)
Re: detect a missing resource file?
Reply #1 - Dec 13th, 2007, 7:39am
 
does it crash really with a NullPointerException? or just display a warning message? reference says :

Quote:
If an image is not loaded successfully, the null value is returned and an error message will be printed to the console. The error message does not halt the program, however the null value may cause a NullPointerException if your code does not check whether the value returned from loadImage() is null.

http://processing.org/reference/loadImage_.html

performing a basic test (!= null) avoids the throwing of a NullPointerException (however the warning message saying the image is missing is still displayed) :

Code:
PImage img = loadImage("image.png");

if (img != null) {
image(img, 0, 0);
}
Re: detect a missing resource file?
Reply #2 - Dec 13th, 2007, 7:49am
 
You can also check for file existence before you load:

Code:

boolean fileExists(String absoluteFilePath){
File file = new File(absoluteFilePath);
if(file.exists())
return true;
else
return false;
}


It's a bit silly, but it works.
Page Index Toggle Pages: 1