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 › weird error on simple code
Page Index Toggle Pages: 1
weird error on simple code (Read 658 times)
weird error on simple code
Dec 21st, 2006, 5:44pm
 
Hi im trying to fill the applet i'm making with a random amount of rectangles at random positions, but in such a way that it does not fill up too much of the applet.

to do that i wrote this code, which is to be executed in the setup() function.

void buildTown(){
   int amountFilled = 0;
   while (amountFilled < ((width*height)/2.2)){
     int xlengte = int(random(5, 60));
     int x = int(random(1, width - xlengte - 5));
     int ybreedte = int(random(5, 60));
     int y = int(random(1, height - ybreedte - 5));
     addBuilding(x,y,x + xlengte,y + ybreedte);
     updatePixels();
     amountFilled = computeSpace();
   }
 }
 
 int computeSpace(){
   int returnvalue = 0;
   loadPixels();
   for(int i = 0; i < pixels.length; i++){
     if(pixels[i] == Cwall){returnvalue++;}
   }
   return returnvalue;
 }

the compiler now complains that there is a nullpointer exception in updatePixels(). Without updatePixels() it goes into an eternal loop, can anyone help me with what's going wrong here?

thanks in advance
Re: weird error on simple code
Reply #1 - Dec 21st, 2006, 9:38pm
 
looking at your code you call updatePixels before loadPixels .. might that be the problem?

if not, can you post the complete code?

btw. i don't think it's a really good idea to work on the pixels-array inside setup(). ( see below )
Re: weird error on simple code
Reply #2 - Dec 22nd, 2006, 3:43pm
 
it's ok to work with pixels[] inside setup(), since setup is the equivalent of draw being run the first time (and that's what it's for).

however you will need to use loadPixels() before ever messing with the pixels[] array.
Page Index Toggle Pages: 1