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 › Null Pointer Exception with 2D objects arrays
Page Index Toggle Pages: 1
Null Pointer Exception with 2D objects arrays (Read 1187 times)
Null Pointer Exception with 2D objects arrays
Feb 8th, 2010, 1:59pm
 
Quote:
public square[][] sqr;
void setup()
{
 size(1000,800);
 sqr  = new square[10][10];
}

void draw()
{
 sqr[0][0].Type();
}

public class square
{
 int type;

 void Type(int typeIn) { type = typeIn;}

 int Type() {return type;}
 
}


I try to run this and I get a Null Pointer Exception...This is a cut down version of a fully working big program. Thanks.
Re: Null Pointer Exception with 2D objects arrays
Reply #1 - Feb 8th, 2010, 2:14pm
 
look at this array of objects example. you miss some important parts in setup. you need to initialize every instance not only 10,10
in this case use 2 loops like in the example.
http://processing.org/learning/basics/arrayobjects.html
Re: Null Pointer Exception with 2D objects arrays
Reply #2 - Feb 8th, 2010, 2:15pm
 
Code:

// declare the array
public square[][] sqr;

void setup()
{
// initialize the ARRAY
sqr  = new square[10][10];

// initialize each square object
for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 10; j++) {
    sqr[i][j] = new square();
  }
}
}
....

Re: Null Pointer Exception with 2D objects arrays
Reply #3 - Feb 9th, 2010, 9:13am
 
Many thanks, I wasnt clear that I needed to define every instace post intailisation.
Page Index Toggle Pages: 1