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 › Yet Another NullPointerException
Page Index Toggle Pages: 1
Yet Another NullPointerException (Read 722 times)
Yet Another NullPointerException
Dec 31st, 2009, 11:46am
 
Hey folks.
I am one of the many newbs who gets a NullPointerException when trying to make an array of objects.
My complete code is at the end; below are the parts that are related to the exception.

Code:
//important system variables
int cellX = 10; //dimensions of a cell
int cellY = 10;
int cellsWide = 5; //dimensions of the world
int cellsTall = 5;

//working variables
Cell[][] world = new Cell[cellsWide][cellsTall];

void setup(){  
 size(cellX*cellsWide,cellY*cellsTall);
 background(deadCell);
 DrawGrid();
}

void draw(){
 for(int i=0;i<cellsWide;i++){
   for(int j=0;j<cellsTall;j++){
     //display current state
     if(world[i][j].state){ //exception occurs here
       println("y");
     }
   }
 }
}


I've looked at several other threads- http://processing.org/discourse/yabb2/num_1251480942_seems_similar__but_the_solutions_don.html't seem to work; initializing the world[][] array before setup() doesn't fix it, nor does declaring it outside setup and initializing the object inside.  Where is the right place for the line

Code:
Cell[][] world = new Cell[cellsWide][cellsTall]; 


?

Thanks!

Full code below.  First, conway2.pde:
Code:
//important system variables
int cellX = 10; //dimensions of a cell
int cellY = 10;
int cellsWide = 5; //dimensions of the world
int cellsTall = 5;
int generation =3000 ; //time length to display a generation (ms)

//aesthetic values
color deadCell = #CCCCCC;
color liveCell = #FFFFFF;
color gridColor = #000000;

//working variables
Cell[][] world = new Cell[cellsWide][cellsTall]; //dimensions are gridX,gridY
int time;

void setup(){
 /* //important system variables
 cellX = 10; //dimensions of a cell
 cellY = 10;
 cellsWide = 5; //dimensions of the world
 cellsTall = 5;
 generation = 3; //time length to display a generation
 
 //aesthetic values
 deadCell = #CCCCCC;
 liveCell = #FFFFFF;
 gridColor = #FFFFFF;*/
 
 size(cellX*cellsWide,cellY*cellsTall);
 background(deadCell);
 //world = new Cell[cellsWide][cellsTall]; //initializes all the cells
 time=0;
 DrawGrid();
}

void DrawGrid(){
 stroke(gridColor);
 for(int i=1;i<cellsWide;i++){
   line(i*cellX,0,i*cellX,height);
 }
 for(int i=1;i<cellsTall;i++){
   line(0,i*cellY,width,i*cellY);
 }
 
}

void draw(){
 for(int i=0;i<cellsWide;i++){
   for(int j=0;j<cellsTall;j++){
     //display current state
     if(world[i][j].state){
       println("y");
     }
   }
 }
 DrawGrid();
}


and then cell.pde:
[code]class Cell{
 boolean state; //0 is dead, 1 is alive
 int gridX; //X coordinate on the grid
 int gridY; //Y coordinate on the grid
 boolean nextState;
 
 void showState(int myX, int myY){
   if(state){
     stroke(liveCell);
   }else{
     stroke(deadCell);
   }
   rect(myX*cellsWide,myY*cellsTall,cellX,cellY);
 }
 
 Cell(){
   if(round(random(0,1)) == 0){
     state = false;
   }else{
     state = true;
   }
 }
 
}[/cell]
Re: Yet Another NullPointerException
Reply #1 - Dec 31st, 2009, 12:31pm
 
The confusion is caused by using an array objects; for instance this code will create a 10x10 arrays of ints - no problem.

Code:

int[][] a = new [10][10];


this code
Code:

Cell[][] world = new Cell[cellsWide][cellsTall];

creates a 2D array of Cell object references not a 2D array of Cell objects

The solution is in setup() include the following statements
Code:

// Create array of references
world = new Cell[cellsWide][cellsTall];
// populate the array with actual objects
for(int i = 0; i < cellsWide; i++){
  for(int j = 0; j < cellsTall; j++){
     world[i][j] = new Cell();
  }
}

and above setup() change this
Code:
//working variables
Cell[][] world = new Cell[cellsWide][cellsTall];

to
Code:
//working variables
Cell[][] world;


Smiley
Re: Yet Another NullPointerException
Reply #2 - Dec 31st, 2009, 2:29pm
 
Quark is right, of course.
I just wanted to point out that the arrays are actually similar, initialized with default values: for the array of ints, the default is 0 (zero); for the array of objects, the default of the references is null, ie. no reference (yet).
Re: Yet Another NullPointerException
Reply #3 - Jan 21st, 2010, 1:31pm
 
Same problem; I totally forgot to populate my array. Thanks Quark
Page Index Toggle Pages: 1