开发者

MVC project can't compile due to an unknown stack overflow error [duplicate]

This question already has answers here: Circular Dependency in classes and StackOverflow Error (1 answer) What is a StackOverflowError? (15 answers) Closed 11 hours ago.

When tryig to execute this Java MVC project, an exception occurs which gives Stack Overflow error as following:

Exception in thread "main" java.lang.StackOverflowError
    at controlador.Controlador.<init>(Controlador.java:19)
    at vista.Vista.<init>(Vista.java:20)
        at controlador.Controlador.<init>(Controlador.java:19)
    at vista.Vista.<init>(Vista.java:20)...

Problem is: that's just where I start the declaration of the View and the Controller respectively:

In Vista line-20 there's Controlador c = new Controlador(); declaration

package vista;


import controlador.Controlador;
import static java.lang.System.out;
import java.util.List;
import modelo.Figura;

public class Vista {

    Controlador c = new Controlador();

    public void inicioPrograma() {
        c.comprobarBin();
        runMenuPrincipal();

    }

    private void runMenuPrincipal(){

/*** rest of the code***/

And the Controller's code, in line-19 there's Vista v = new Vista() declaration:

public class Controlador {

 
    Vista v = new Vista();
    Modelo m = new Modelo();

    public void comprobarBin() {
        if (m.binExiste()) {
            v.showMessage("Se cargará el bin\n", true);
            if(m.importarBin()){
                    v.showMessage("Importación realizada con exito",true);
                }
        else{
                    v.showMessage("Error en la importación",false);
                }

        } else {
            v.showMessage("No existe el fichero bin\n", false);
            v.showMessage("Debe cargar otro fichero\n", false);

        }
    }
/*** rest of the code***/

Model is here:

package modelo;
/**imports**/
public class Modelo {

    
    Controlador c = new Controlador();
    final String fichero = "datos_figuras";
    final String archivo = "figuras.bin";
    final String archivoCSV = "figuras.csv";
    private List<Figura> figuras;

    public List<Figura> getFiguras() {
        return figuras;
    }

    public boolean binExiste() {
        File f = Rutas.fileToFileInFolderOnDesktop(fichero, archivo);
        return f.exists();
    }

    public boolean importarBin() {
        File f = Rutas.fileToFileInFolderOnDesktop(fichero, archivo);
        FileInputStream fis;
        BufferedInputStream bis;
        ObjectInputStream ois = null;

    开发者_如何学运维    try {
            fis = new FileInputStream(f);
            bis = new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);

            this.figuras = (ArrayList<Figura>) ois.readObject();

        } catch (FileNotFoundException ex) {
            return false;
        } catch (IOException | ClassNotFoundException ex) {
            return false;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Modelo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
        return true;
    }
/*** rest of the code***/

As for the Main Class, it looks like this:

package galeriaMain;
import vista.Vista;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        Vista v = new Vista();
        v.inicioPrograma();        
    }
    
}

The only thing that happens automatically is checking if a binary file exists, and then when the menu starts to run. The rest of operations depend on the user to run. Any idea of why is this happening?

I've seen some other answers telling it's because of recursion: I can see that. However, I can't really tell what's causing it because of the conditions.I don't really know what's causing it to loop. If the error happened elsewhere, it'd be easier to tell, but just that it's giving me no clues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜