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 › existance of file / URL check
Page Index Toggle Pages: 1
existance of file / URL check (Read 1384 times)
existance of file / URL check
Jul 7th, 2005, 5:18pm
 
Using the loadImage() function on an image that doesn't exist throws an exception and halts processing.

Would it be possible for loadImage() to return a boolean regarding the existence of the file / url it's being fed?
Re: existance of file / URL check
Reply #1 - Jul 7th, 2005, 5:54pm
 
does this work? (haven't tried it):
Code:

try{
loadImage("xxx.jpg");
}catch(Exception e){
println("Your xxx.jpg file does not exist");
}
Re: existance of file / URL check
Reply #2 - Jul 7th, 2005, 5:55pm
 
I'm not implying anything about the nature of the picture, don't get missunderstood with the name of the file.  jejejeje
Re: existance of file / URL check
Reply #3 - Jul 16th, 2005, 1:28am
 
does it throw an exception or return null, which then causes a NullPointerException?

we can't exactly return a boolean because returning the image is probably more useful Wink
Re: existance of file / URL check
Reply #4 - Jul 24th, 2005, 10:48pm
 
I am seeing this same problem.  Processing halts and will not continue when a file is not found with loadImage method.  

I can catch the exception, but I can not seem to continue processing.


Here is my code:
Code:

boolean getImageById(int Id) {

String f = "thumb" + Id + ".jpg";

try {
PImage p = loadImage(f);
doSomethingWith(p);
} catch (Exception e) {
println(f+" file not found");
return false;
}
return true;
}


Here is the error I get when a file is missing:
-----------------------------------------------------
java.io.IOException: openStream() could not open D:\folded\thumb161.jpg
java.io.IOException: openStream() could not open D:\folded\thumb161.jpg



at processing.core.PApplet.openStream(PApplet.java:3168 )
java.lang.ArrayIndexOutOfBoundsException: -1


at processing.app.Runner.message(Runner.java:491)


at processing.app.MessageSiphon.run(MessageSiphon.java:60 )


at java.lang.Thread.run(Unknown Source)


D:\folded\thumb161.jpg file not found


-----------------------------------------------------

This is Processing 0091 in Windows XP, JRE 1.4.2.  

Thanks for any help!
jared

Re: existance of file / URL check
Reply #5 - Sep 16th, 2005, 7:37am
 
does anyone know any more about checkign to see if a file can be loaded or not? i'm having a similar situation.
Re: existance of file / URL check
Reply #6 - Sep 16th, 2005, 9:25am
 
I tried the code above, and it seemed to work fine:
Code:

try{
PImage img = loadImage("http://wliia.org/bob.gif");
}catch (Exception e){
println(e);
}


I only get the exception printed out, and the program continues.. :)

-seltar
Re: existance of file / URL check
Reply #7 - Sep 16th, 2005, 9:55am
 
thanks, i tried it earlier but it didn't work, but now it does.  meh, tech-voodoo
Re: existance of file / URL check
Reply #8 - Sep 18th, 2005, 9:30am
 
The same problem.
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1126457669;start=0#5

Code:


boolean loadWithoutException(String filename)
{
 URL url = null;
 try {
   url = new URL(filename);
   if (url != null) {
     return true;
   }
 }
 catch (MalformedURLException e) {
 }  // ignored
 url = getClass().getResource(filename);
 if (url != null) {
   return true;
 }

 url = getClass().getResource("data/" + filename);
 if (url != null) {
   return true;
 }        

 try {
   try {
     // look inside the sketch folder (if set)
     String location = this.folder + File.separator + "data";
     File file = new File(location, filename);
     if (file.exists()) {
       return true;
     }
   }
   catch (Exception e) {
   }  // ignored

   try {
     File file = new File("data", filename);
     if (file.exists()) {
       return true;      
     }
   }
   catch (Exception e) {
   } // ignored

   try {
     File file = new File(filename);
     if (file.exists()) {
       return true;
     }
   }
   catch (Exception e) {
   } // ignored
 }
 catch (SecurityException se) {
 }  // online, whups    
 return false;
}

Page Index Toggle Pages: 1