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
Page Index Toggle Pages: 1
null pointer exception (Read 627 times)
null pointer exception
Mar 24th, 2010, 9:30am
 
so im getting a null pointer exception in my setup when i'm trying to add to an arraylist. i can't see any reason for getting the exception though its probably something stupid im over looking any help would be greatly appreciated. thanks in advance

Code:
ArrayList blocks;
PVector[] adjlocs = new PVector[4];

void setup(){
 size(960,600);
 for(int i = 0; i< 10;i++){
   for(int x = 0; x< 10;x++){
     blocks.add(new Block(width/2+i*25, height/2+x*25, 25, int(random(0,2))));
   }
 }
 println(blocks.size());
}

void draw(){
 background(0);
 for(int i=0;i<blocks.size();i++){
   Block tempblock = (Block) blocks.get(i);
     if(tempblock.activated == false){
       tempblock.display();
     }
     else{
       tempblock.nodisplay();
     }
   }
 }


void mouseClicked(){
 for(int i=0;i<blocks.size();i++){
   Block tempblock = (Block) blocks.get(i);
     if(dist(mouseX, mouseY, tempblock.blockx, tempblock.blocky) <=12.5){
       tempblock.activated = true;
       //println(i+", "+x);
       //blockcheck(i);
     }
   }
 }

class Block{
 float blockx;
 float blocky;
 float blocksize;
 boolean activated;
 int type;

 Block(float x, float y, float _blocksize, int _type){
   blockx = x;
   blocky = y;
   blocksize = _blocksize;
   activated = false;
   type = _type;
 }

 void nodisplay(){
   
 }

 void display(){
   if(activated == false){
     strokeWeight(1);
     if(this.type == 0){
       stroke(0,80,250);
     }
     else if(this.type == 1){
       stroke(250,45,45);
     }
   }
   else if(activated == true){
     strokeWeight(2);
     if(this.type == 0){
       stroke(0,80,250);
     }
     else if(this.type == 1){
       stroke(250,45,45);
     }
   }
   if(this.type == 0){
     fill(0,80,250,75);
   }
   else if(this.type == 1){
     fill(250,45,45,75);
   }
   rectMode(CENTER);
   rect(blockx, blocky, blocksize,blocksize);

 }
}


Re: null pointer exception
Reply #1 - Mar 24th, 2010, 9:39am
 
ArrayList blocks;

that isn't enough

ArrayList blocks = new ArrayList();

that is.

http://www.java-tips.org/java-se-tips/java.lang/use-of-arraylist-class.html
Re: null pointer exception
Reply #2 - Mar 24th, 2010, 10:01am
 
that was it. thanks
Page Index Toggle Pages: 1