Eli
Eli

Reputation: 65

Stack using Processing (based on JAVA)

I am having problems using stack method in Processing. I want to store in a stack coordinates and then get them out.

Here is a code involving what I want

  import java.util.ArrayList;
  import java.util.Random;
  import java.lang.Math;
  import java.util.Stack;

  Stack<Celda> pila=new Stack<Celda>();

   int f1, f2, a, b, c, d, e, f, p, w, w1, w2,g,k;
    int [][] laberinto;
   int [] direcciones = new int [4];
  int cols, rows;
  int alto = 50;         
  int ancho = 50;        
  int celda = 10;          
  color co = color(175, 100, 220);
  Celda[][] coordenada;
  Celda[][] actual;

  void setup(){
  size( ancho*celda, (alto*celda));
  background(50);
  cols = width/celda;
  rows = height/celda;
  w = 10;
  w1 = w - 1;
  w2 = 2*w - 1;
  f1= int(random(0,cols));
  f2= int(random(0,rows));
  c = f1/2;
  d = f2/2;
  e = cols-1;
  g=rows-1;
  laberinto = new int [cols][rows]; 
  coordenada = new Celda[cols][rows];
  actual = new Celda[cols][rows];

  for(int i = 0; i < alto*celda; i++)
  for(int j = 0; j < ancho*celda; j++){
  fill(50);
  rect(i*celda,j*celda,celda,celda);
  }
  }

  void draw(){

  p = 0;

  if (c > 0) { if (laberinto[c-1][d] == 0) {    direcciones[p++] = 1;  }}
  if (d > 0) { if (laberinto[c][d-1] == 0) {   direcciones[p++] = 2;  }}
  if (c < e) { if (laberinto[c+1][d] == 0) {    direcciones[p++] = 4;  }}
  if (d < g) { if (laberinto[c][d+1] == 0) {   direcciones[p++] = 8;  }}


  if (p > 0) {//cuando p es mayor a cero hay una celda vacía
  p = direcciones[floor(random(p))];
  laberinto[c][d] += p;
  switch(p) {

   case 1:
   rect(c*w-w, d*w, w, w);
   laberinto[--c][d] = 1;
   pila.push(coordenada[c][d]);
   fill(co);
   stroke(co);
   line(c*w+w, d*w+1, c*w+w, d*w+w-1);
   stroke(0);
   k=1;
   break;

   case 2:
   rect(c*w, d*w-w, w, w);
  laberinto[c][--d] = 1;
   pila.push(coordenada[c][d]);

  fill(co);
  stroke(co);
  line(c*w+1, d*w+w, c*w+w-1, d*w+w);
  stroke(0);
  k=2;

  break;
  case 4:
  rect(c*w+w, d*w, w,w);

  fill(co);
  laberinto[++c][d] = 1;
  pila.push(coordenada[c][d]);

  stroke(co);
  line(c*w, d*w+1, c*w, d*w+w-1);
  stroke(0);
  k=3;
  break;

case 8:
  rect(c*w, d*w+w, w, w);

  fill(co);
  laberinto[c][++d] = 1;
  pila.push(coordenada[c][d]);

  stroke(co);
  line(c*w+1, d*w, c*w+w-1, d*w);
  stroke(0);
  k=4;
  break;
  }
  } else
   {  int a=1,b=1;

   actual[a][b]=pila.pop();
   c = actual[a][b].celdaX;
   d = actual[a][b].celdaY;
    noStroke();
    fill(255);
  rect(c*w+1, d*w+1, w-1, w-1); 

}  
}

class Celda{
int celdaX, celdaY;
Celda(int celdaX, int celdaY){
this.celdaX = celdaX;
this.celdaY = celdaY;
 }
}

I get the Error null pointer exception.

Can somebody tell how to fix this?

Thanks!

Upvotes: 1

Views: 1130

Answers (1)

Eran
Eran

Reputation: 393936

Assuming all your main method does is call setup and then draw :

pila.push(coordenada[c][d]); // c and d are 0, coordenada[0][0] is null
actual[a][b]=pila.pop(); // is null
c = actual[a][b].celdaX; // throws NullPointerException

You must assign a non null reference to coordenada[0][0] to avoid the exception.

Upvotes: 1

Related Questions