1

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!

6
  • 1
    Where is your main method? Commented Feb 21, 2015 at 5:52
  • might be here c = actual[a][b].celdaX; Commented Feb 21, 2015 at 5:54
  • @Prashant Yes, the error is in that line, I dont know how to fix it Commented Feb 21, 2015 at 5:57
  • We need to see the input that you are using when you are getting the exception. Commented Feb 21, 2015 at 5:58
  • the value of actual[a][b] is null at this point so its throwing exception. your pila.pop(); is returning null here check this method. Commented Feb 21, 2015 at 5:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you tell me how to assign that reference?
@Eli you can make an integer method to find out if a cell is valid, something like ` int index(int s, int t) { if (s<0 || t<0 || s>cols-1 || t>rows-1) { return -1; } return s+t*cols; }` and then call it in an if statement, if -1 is not returned do stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.