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 & HelpPrograms › What is wrong in this
Page Index Toggle Pages: 1
What is wrong in this? (Read 872 times)
What is wrong in this?
Nov 30th, 2007, 4:40pm
 
Hi, I'm new to Processing and have made a very small program but am unable to find what is wrong in this.

I actually have some random files kept  in my data folder C0.pts, C23.pts, C148.pts etc. (any number from 0 to 220 after C) which have some data. What my program does is try to open the files one by one on each key press, using my Polygon class.

Now, I know that not all numbers from 0 to 220 are there, so I wrote a try catch block so that whichever file name is not found, it goes in the catch block and continues. ( I know what I'm doing is a very crude way of doing it but that's all right :) )

But, it opens the file C0.pts successfully, and tries only upto C5.pts and then stops!! I don't know why should it stop trying before 220!!




Anyway, here's the program. I'm not supplying the class Polygon, you can replace that by any random file opening process. :-




//Code starts from here

boolean fileNotFound=false;
int i;
Polygon C;
boolean keyLocked=false;

void setup()
{
 size(800,800);
 size(800,800);
 size(800,800); //Problem where processing window is opened with the default 100,100 size.
 C=new Polygon();


}

void draw()
{

 if(keyPressed && !keyLocked)
 {
   keyLocked=true;
   fileNotFound=true;
   while(fileNotFound)
   {
     try
     {

       println(i);
       C.loadPts("C"+i+".pts");
       C.drawCurve();
       fileNotFound=false;
     }
     catch(Exception ex)
     {
       println("Exception");
       fileNotFound=true;      
     }

     i++;
     if(i>221) i=0;      
     println("Came here");

   }

 }

}

void keyReleased()
{
 keyLocked=false;//To take the key hit only once before releasing it
}






here is the output of the program:

<on one key hit>
0
Came here


<on 2nd key hit - exact copy paste from here including this message>

The file "C1.pts" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

1
Exception
Came here
2
Exception
Came here
3
Exception
Came here
4
Exception
Came here
5

________________________________________
________________________________________

Now I don't understand, why does it stop after 5? It should continue upto file C23.pts!! (Also, it hangs after showing the above messages we can't close it by close button)
I tried if Processing take a cautionary 'stop' after a few try catches, but it is not the case with simple exceptions I threw. (It must not !)

I may be doing something very silly but I don't understand it so I thought may be it is something Processing specific.
Please help!
Re: What is wrong in this?
Reply #1 - Nov 30th, 2007, 5:07pm
 
are you on a mac? if so, there seems to be a problem on macs where the sketch may die if too many exceptions come through (looking into that one).

if you're on a pc, then that shouldn't be happening. also, the size() bug is fixed in recent releases, don't do that multiple size() command bit anymore.

looking at the code i'd most suspect your while() loop. while() tends to be really dangerous, since it may not exit if the condition never comes 'true'. better to avoid it and use another method.

best solution in the end is to either 1) list the files in the directory and read them (examples elsewhere on the board) or 2) make an array with the list of files you want to use, and put that in the program.
Re: What is wrong in this?
Reply #2 - Nov 30th, 2007, 5:57pm
 
Hi,
I'm not using a Mac, it's a laptop running on Windows Vista.
To test this 'many exceptions' thing only, I tried another small program in which what all I do is throw exceptions in the draw() and catch them each time. It went quite well upto 2000 at least when I stopped it. (It looked good)

May be, it can catch multiple exceptions of simple type and not of the 'File not found' type? (which is bad !!). I mean, I know that my way of doing the job is not good, but what has processing to give and take with that? :-)

Anyway, I'll try listing all the files in the directory and then loop through them. In the mean time, this is something for you people to think upon.
Page Index Toggle Pages: 1