0

When I run my code I get java.lang.StackOverflowError

I would be more than appreciative if someone can tell me what's wrong.

This is my code:

int cantPuntos;
static int HEIGHT = 625;
static int WIDTH = 1000;
Point[] puntos;
static BufferedImage buff;

public static void main(String[] args) {

      JFrame frame = new JFrame();
      frame.getContentPane().add(new Main());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(WIDTH, HEIGHT);
      frame.setVisible(true);

}
public void paint(Graphics g){
    buff = salPimienta();
    Image img = buff;
    g.drawImage(img, 0,0,this);
}


//Cantidad de pixeles dependiendo porcentaje
public int cantPixelSP(int porcentaje){
    cantPuntos = ((HEIGHT*WIDTH)*porcentaje)/100;
    puntos = new Point[cantPuntos];
    System.out.println("Cont: "+cantPuntos);
    return (int) (cantPuntos);
}

public int generarRandomW(){
    return (int) (Math.random()*(WIDTH+1));
}

public int generarRandomH(){
    return (int) (Math.random()*(HEIGHT+1));
}

//Genera puntos y los guarda en un arreglo de tipo Point 
public void generarPuntos(int cont){
    if(cont<cantPuntos){
        puntos[cont]=new Point(generarRandomW(),generarRandomH());
        cont++;
        generarPuntos(cont);
    }

}

//Pinta los puntos de blanco o negro
public Graphics pintar(Graphics g, int cont){
    if(cont<puntos.length){
        if(cont<= (int) Math.floor(puntos.length/2)) g.setColor(Color.black);
        else g.setColor(Color.WHITE);
        g.drawRect((int) puntos[cont].getX(), (int) puntos[cont].getY(), 1, 1);
        pintar(g,cont+1);
    }
    return g;
}

//Crea sal y pimienta
public BufferedImage salPimienta(){
    cantPixelSP(1);
    System.out.println("Cant: "+cantPuntos+" vector: "+puntos.length );
    generarPuntos(0);
    BufferedImage bufferedImage = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
    try {
        System.out.println(getClass().getResource("/Paris.jpg"));
        bufferedImage = ImageIO.read(getClass().getResource("/Paris.jpg"));
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    Graphics g = bufferedImage.getGraphics();
    g = pintar(g, 0);
    return bufferedImage;
}

I need to add noise to an Image. The StackOverFlowError occurs in this function:

public void generarPuntos(int cont){
    if(cont<cantPuntos){
        puntos[cont]=new Point(generarRandomW(),generarRandomH());
        cont++;
        generarPuntos(cont);
    }

}

I will appreciate your help.

  • 4
    Looking at your code, you try to recurse down 6,250 times. You can [increase the stack size](http://stackoverflow.com/q/3700459/1270789), but I think you would be much better off just rewriting the code as `for (int cont = 0; cont < cantPuntos; cont++) { tos[cont]=new Point(generarRandomW(),generarRandomH()); }`. – Ken Y-N Apr 17 '17 at 03:39
  • if the total amount of stuff on the stack exceeds that value, you'll get that error, You can increase the size by -Xss option But I recommend rewriting your code – Fady Saad Apr 17 '17 at 03:49
  • I would not prefer recursion for generarPuntos method as @ Ken Y-N says. – thread-game Apr 17 '17 at 05:42

1 Answers1

0

use this instead

//Genera puntos y los guarda en un arreglo de tipo Point

public void generarPuntos(int cont){

while(cont<cantPuntos){

    puntos[cont]=new Point(generarRandomW(),generarRandomH());

    cont++;
}}

since each function call and its parameters are saved in stack memory

MohanaPriyan
  • 218
  • 3
  • 9