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 › pulling a null pointer exception and not sure why
Page Index Toggle Pages: 1
pulling a null pointer exception and not sure why (Read 410 times)
pulling a null pointer exception and not sure why
Apr 6th, 2010, 7:27am
 
i'm getting a null pointer exception but as far as i can tell i shouldn't be, obviously there is something that i'm missing can anyone help me out and figure out what i'm doing wrong? the NPE is popping up in my gravity method of my block class.

Code:
int pointer = 0;
float maxRow = 10;
float maxCol = 10;
int blocksize = 25;
PVector[] adjlocs = new PVector[int(maxCol*maxRow)];
Block[][] blocksList = new Block[int(maxCol)][int(maxRow)];

void setup(){
 size(960,600);
 /////////////populating blocks in blocks blocksList matrix
 for(int i = 0; i< maxCol;i++){
   for(int x = 0; x< maxRow;x++){
     blocksList[i][x] = new Block(width/2+i*25, height/2+x*25, blocksize, int(random(0,2)));
   }
 }
}

void draw(){
 background(0);
 for(int i=0;i<maxCol;i++){
   for(int x=0;x<maxRow;x++){
     //    Block tempblock = (Block) blocks.get(i);
     //    if(tempblock.activated == false){
     //      tempblock.display();
     //      tempblock.gravity();
     //    }
     //    else{
     //      tempblock.display();
     //      deleteblocks();
     //    }
     if(blocksList[i][x] != null){
       blocksList[i][x].display();
       blocksList[i][x].gravity();
     }
   }
 }
}


void mouseClicked(){
 for(int i = 0;i<maxCol; i++){
   for(int x=0; x<maxRow; x++){
     if(blocksList[i][x] != null){
       if(dist(blocksList[int(i)][int(x)].blockx,blocksList[int(i)][int(x)].blocky,mouseX,mouseY) <=12.5){
         blocksList[i][x].activated = true;
         checkblock(i,x);
         for(int t = 0; t<adjlocs.length;t++){
           if(adjlocs[t] != null){
             checkblock(int(adjlocs[t].x), int(adjlocs[t].y));
             adjlocs[t] = null;

           }
         }
       }
     }
   }
 }
 removeBlocks();
 pointer = 0;
}

class Block{
 public float blockx;
 public float blocky;
 private float blocksize;
 public boolean activated;
 public 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);

 }

 void gravity(){
   float grav = .01;
   for(int i=0;i<maxCol-1;i++){
     for(int x=0; x< maxRow-1;x++){
       if(dist(blocksList[i][x].blockx,blocksList[i][x].blocky,blocksList[i][x+1].blockx,blocksList[i][x+1].blocky) > 25){
         blocksList[i][x].blocky += grav;
       }
     }
   }
 }
}

void checkblock(int col, int row){

 Block tempblock = blocksList[int(col)][int(row)];
 Block topblock = blocksList[int(col)][int(row)];
 Block bottomblock = blocksList[int(col)][int(row)];
 Block leftblock = blocksList[int(col)][int(row)];
 Block rightblock = blocksList[int(col)][int(row)];
 //assign right
 if(col < maxCol-1 && blocksList[int(col+1)][int(row)] != null){
   rightblock = blocksList[int(col+1)][int(row)];
   if(rightblock.type == tempblock.type){
     if(rightblock.activated != true){
       rightblock.activated = true;
       adjlocs[pointer] = new PVector(int(col+1), int(row));
       pointer++;
     }
   }
 }
 //assign bottom
 if(row < maxRow-1 && blocksList[int(col)][int(row+1)] != null){
   bottomblock = blocksList[int(col)][int(row+1)];
   if(bottomblock.type == tempblock.type){
     if(bottomblock.activated != true){
       bottomblock.activated = true;
       adjlocs[pointer] = new PVector(int(col), int(row+1));
       pointer++;
     }
   }
 }
 //assign left
 if(col != 0 && blocksList[int(col-1)][int(row)] != null){
   leftblock = blocksList[int(col-1)][int(row)];
   if(leftblock.type == tempblock.type){
     if(leftblock.activated != true){
       leftblock.activated = true;
       adjlocs[pointer] = new PVector(int(col-1), int(row));
       pointer++;
     }
   }
 }
 //assign top
 if(row != 0 && blocksList[int(col)][int(row-1)] != null){
   topblock = blocksList[int(col)][int(row-1)];
   if(topblock.type == tempblock.type){
     if(topblock.activated != true){
       topblock.activated = true;
       adjlocs[pointer] = new PVector(int(col), int(row-1));
       pointer++;
     }
   }
 }
}

class Checkint{
 int posvalue;
 
 Checkint(int temp){
   posvalue = temp;
 }
}
void removeBlocks(){
 for(int i=0;i<maxCol;i++){
   for(int x=0;x<maxRow;x++){
     if(blocksList[i][x].activated == true){
       blocksList[i][x] = null;
     }
   }
 }
}


Re: pulling a null pointer exception and not sure why
Reply #1 - Apr 6th, 2010, 8:32am
 
removeBlocks() => blocksList[i][x] = null;

Also you do too much int() calls, IMHO.
Page Index Toggle Pages: 1