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 › Is init() a keyword
Page Index Toggle Pages: 1
Is init() a keyword? (Read 622 times)
Is init() a keyword?
Jun 30th, 2009, 11:38am
 
Hi...I'm new to the boards (and Processing). Just a quick question to start my journey:

I built a function called init(). When called from setup(), it did what it was supposed to, but then gave a nullpointerexception (working with arrays at the time), and exited before proceeding to draw().  

For example, the following will NOT print out width and height:

Code:
void setup(){
 size(10,10);
 init();
}

void draw(){
}

void init(){
 println(width + "," + height);
}


However, rename init() to something else, and it works. I'm assuming init() is a keyword, although I haven't found seen it in the processing references. I'm guessing it's a java keyword and may be used in libraries. Does that sound right?
Re: Is init() a keyword?
Reply #1 - Jun 30th, 2009, 12:48pm
 
Hi,

when you write a sketch, you are inside an Object called PApplet. Since init is a method of PApplet you are effectively overriding this method. This is ok, as long as you call the parent method, like so:

void init(){
 super.init();
 println(width + "," + height);
}

If you are interested to see the full list of methods, here it is:
http://dev.processing.org/reference/core/javadoc/processing/core/PApplet.html
Re: Is init() a keyword?
Reply #2 - Jun 30th, 2009, 12:52pm
 
Try to choose another name for init() --> initWhatEver()

You have a name collision. In  PApplet there's a hidden function also named init(), when you create your own init(), in fact you override PApplet.init(), and nothing is working anymore.

Try this;

Code:
void setup(){
 size(10,10);
 initX();
}

void draw(){
}

void initX(){
 println(width + "," + height);
}
Re: Is init() a keyword?
Reply #3 - Jun 30th, 2009, 12:54pm
 
Ok, alvaro already replied...
Page Index Toggle Pages: 1