Problems with "Pluviometer" Hashmap, List ... java.lang.StackOverflowError

0

I am very fish in this subject and it is not resolved. I get the following error:

Exception in thread "main" java.lang.StackOverflowError
    at Pluviometro.<init>(Pluviometro.java:11)

Program:

public interface Meses {

    public static final String MES_ENERO = "ENERO";
    public static final String MES_FEBRERO = "FEBRERO";
    public static final String MES_MARZO = "MARZO";
    public static final String MES_ABRIL = "ABRIL";
    public static final String MES_MAYO = "MAYO";
    public static final String MES_JUNIO = "JUNIO";
    public static final String MES_JULIO = "JULIO";
    public static final String MES_AGOSTO = "AGOSTO";
    public static final String MES_SEPTIEMBRE = "SEPTIEMBRE";
    public static final String MES_OCTUBRE = "OCTUBRE";
    public static final String MES_NOVIEMBRE = "NOVIEMBRE";
    public static final String MES_DICIEMBRE = "DICIEMBRE";

}

Pluviometer:

public class Pluviometro implements Meses {

    private HashMap<String, Pluviometro> hm = new HashMap<String, Pluviometro>();
    ArrayList<Integer> pre = new ArrayList<Integer>();

    public Pluviometro() {
        hm.put(MES_ENERO, new Pluviometro());
        hm.put(MES_FEBRERO, new Pluviometro());
        hm.put(MES_MARZO, new Pluviometro());
        hm.put(MES_ABRIL, new Pluviometro());
        hm.put(MES_MAYO, new Pluviometro());
        hm.put(MES_JUNIO, new Pluviometro());
        hm.put(MES_JULIO, new Pluviometro());
        hm.put(MES_AGOSTO, new Pluviometro());
        hm.put(MES_SEPTIEMBRE, new Pluviometro());
        hm.put(MES_OCTUBRE, new Pluviometro());
        hm.put(MES_NOVIEMBRE, new Pluviometro());
        hm.put(MES_DICIEMBRE, new Pluviometro());
    }

    public void putPrecipitacion(String Meses, int litros) {
        pre.add(litros, Integer.parseInt(Meses));
    }

    public String getPre(String Meses) {
        String s = "";
        for(int i = 0; i < pre.size(); i++) {
            s = s + pre.get(i) + " ";
        }
        return s;
    }

    public int totalMes(String Meses) {
        int litros = 0;

        for(int i = 0; i < 30; i++) {
            litros = litros + pre.get(i);
        }
        return litros;
    }

    public int mediaDiaria(String Meses) {
        return Math.round(totalMes(Meses)/30);
    }

    public int mediaMensual(String Meses) {
        return Math.round(totalAnual(Meses)/30);
    }

    public int totalAnual(String Meses) {
        int litros = 0;

        for(int i = 0; i < 365; i++) {
            litros+=pre.get(i);
        }
        return litros;
    }

}

Main:

public class Actividad8 implements Meses {

    /**
     * @param args
     */
    public static void main(String[] args) {


        Pluviometro plv = new Pluviometro();

        //Insertar datos de forma aleatoria
        String[] meses = {MES_ENERO, MES_FEBRERO, MES_MARZO, MES_ABRIL, MES_MAYO, MES_JUNIO, MES_JULIO, MES_AGOSTO, MES_SEPTIEMBRE, MES_OCTUBRE, MES_NOVIEMBRE, MES_DICIEMBRE};
        int mes;
        int litros;
        for(int i = 0; i < 12; i++) {
            mes = (Integer.valueOf(Double.toString(Math.random()).substring(2,4))%12);
            litros = Integer.valueOf(Double.toString(Math.random()).substring(2,4));
            plv.putPrecipitacion(meses[mes], litros);
        }

        //Visualización de los datos
        for(int i = 0; i < 12; i++) {
            System.out.println(meses[i] + ": " + plv.getPre(meses[i])
                    + "    Total mensual: " + plv.totalMes(meses[i])
                    + "    Media diaria: " + plv.mediaDiaria(meses[i])
                    + "    Total anual: " + plv.totalAnual(meses[i])
                    + "    Media mensual: " + plv.mediaMensual(meses[i]));;
        }

    }

}
    
asked by DrWho 23.11.2016 в 23:16
source

1 answer

2

You have an infinite recursion problem.

If you notice in your constructor:

public Pluviometro() {
    hm.put(MES_ENERO, new Pluviometro());
    hm.put(MES_FEBRERO, new Pluviometro());
    hm.put(MES_MARZO, new Pluviometro());
    hm.put(MES_ABRIL, new Pluviometro());
    hm.put(MES_MAYO, new Pluviometro());
    hm.put(MES_JUNIO, new Pluviometro());
    hm.put(MES_JULIO, new Pluviometro());
    hm.put(MES_AGOSTO, new Pluviometro());
    hm.put(MES_SEPTIEMBRE, new Pluviometro());
    hm.put(MES_OCTUBRE, new Pluviometro());
    hm.put(MES_NOVIEMBRE, new Pluviometro());
    hm.put(MES_DICIEMBRE, new Pluviometro());
}

... the constructor of Pluviometro executes the constructor of Pluviometro ( new Pluviometro() ), which in turn executes the constructor of Pluviometro , etc ... infinitely until you receive the StackOverflowException .

Obviously, it is a design problem. But since I do not know what you're trying to do, I can not recommend a specific solution, but I'll simply let you know where the problem is.

    
answered by 23.11.2016 в 23:24