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 › Global array variable
Page Index Toggle Pages: 1
Global array variable ? (Read 436 times)
Global array variable ?
Nov 8th, 2007, 5:40pm
 
What is wrong with this? (I get java.lang.NullPointerException)
----------------------------------------

int[] kk; //<<< I want kk[] to be available globally

void setup() {
 size(120,120);
 noLoop();
 //
 int[] kk={
   11,22,33  }; //<<< kk[] setup at runtime
}//setup()

void draw() {
 print(kk[1]); //<<< kk not available here ???
}//draw()
Re: Global array variable ?
Reply #1 - Nov 8th, 2007, 7:06pm
 
You're setting up two different arrays called kk

Code:
 int[] kk; //<-- this is a global kk

void setup() {
size(120,120);
noLoop();

// int[] kk={ // <-- this is a LOCAL kk
// 11,22,33};
//what you really want is:

kk=new int[]{11,22,33};
//if you specify a type, it creates a new, different variable.
}
Re: Global array variable ?
Reply #2 - Nov 8th, 2007, 7:15pm
 
Exactly : variables declared in a method can only be adressed within this method, otherwise the compiler throws a null exception. This is the concept of scope. To setup a global variable, declare it outside of any method.
Page Index Toggle Pages: 1