StackOverflowError

jumilo

Mitglied
Hallo,
als ich letztens versuchte die Lösung für ein vereinfachtes "Travelling Salesman Problem" zu implementieren löste mein Code ein StackOverflowError aus. Da ich selbst auch nach Recherche noch ratlos bin wollte ich mal um Hilfe bitten.
Es wäre nett wenn mir jemand erklärt was den Error ausgelöst hat und auch ein Lösungsweg vorschlägt.
Danke
jumilo

Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 
 */
public class Towns {
    String zahlenkolonne;
    int count;
    int random;
    boolean zwei = false;
    boolean drei = false;
    boolean vier = false;
    boolean fünf = false;
    boolean sechs = false;
    boolean sieben = false;
    boolean acht = false;
    boolean neun = false;
    boolean zehn = false;
    boolean elf = false;
    boolean zwölf = false;
    boolean dreizehn = false;
    boolean vierzehn = false;
    boolean fünfzehn = false;
    boolean sechszehn = false;
    boolean siebzehn = false;
    boolean achtzehn = false;
    boolean neunzehn = false;
    boolean zwanzig = false;
    boolean einundzwanzig = false;
    boolean zweiundzwanzig = false;
    
    public void start(){
        zahlenkolonne = "";
        count = 0;
        zwei = false;
        drei = false;
        vier = false;
        fünf = false;
        sechs = false;
        sieben = false;
        acht = false;
        neun = false;
        zehn = false;
        elf = false;
        zwölf = false;
        dreizehn = false;
        vierzehn = false;
        fünfzehn = false;
        sechszehn = false;
        siebzehn = false;
        achtzehn = false;
        neunzehn = false;
        zwanzig = false;
        einundzwanzig = false;
        zweiundzwanzig = false;
        random = (int) (Math.random() * 5)+1;
        if (random == 1){
            zwanzig();
        }
        if (random == 2){
            zweiundzwanzig();
        }
        if (random == 3){
            fünfzehn();
        }
        if (random == 4){
            zehn();
        }
        if (random == 5){
            sechszehn();
        }
    }
    public void zwei(){
        count++;
        if (zwei){
            start();
        }
        else{
            zwei = true;
        }
        zahlenkolonne = zahlenkolonne + " 2";
        random = (int) (Math.random() * 5)+1;
        if (random == 1){
            ende();
        }
        if (random == 2){
            fünf();
        }
        if (random == 3){
            neun();
        }
        if (random == 4){
            drei();
        }
        if (random == 5){
            sieben();
        }
    }
    public void drei(){
        count++;
        if (drei){
            start();
        }
        else{
            drei = true;
        }
        zahlenkolonne = zahlenkolonne + " 3";
        random = (int) (Math.random() * 4)+1;
        if (random == 1){
            zwei();
        }
        if (random == 2){
            dreizehn();
        }
        if (random == 3){
            vierzehn();
        }
        if (random == 4){
            acht();
        }
    }
    public void vier(){
        count++;
        if (vier){
            start();
        }
        else{
            vier = true;
        }
        zahlenkolonne = zahlenkolonne + " 4";
        random = (int) (Math.random() * 4)+1;
        if (random == 1){
            acht();
        }
        if (random == 2){
            zehn();
        }
        if (random == 3){
            sechszehn();
        }
        if (random == 4){
            fünf();
        }
    }
    public void fünf(){
        count++;
        if(fünf){
            start();
        }
        else{
            fünf = true;
        }
        zahlenkolonne = zahlenkolonne + " 5";
        random = (int) (Math.random() * 5)+1;
        if (random == 1){
            zwei();
        }
        if (random == 2){
            neun();
        }
        if (random == 3){
            vier();
        }
        if (random == 4){
            sechszehn();
        }
        if (random == 5){
            elf();
        }
    }
    public void sechs(){
        count++;
       if(sechs){
           start();
       }
       else{
           sechs = true;
       }
       zahlenkolonne = zahlenkolonne + " 6";
       random = (int) (Math.random() * 3)+1;
        if (random == 1){
            zwanzig();
        }
        if (random == 2){
            siebzehn();
        }
        if (random == 3){
            zwölf();
        }
    }
    public void sieben(){
        count++;
        if(sieben){
            start();
        }
        else{
            sieben = true;
        }
       zahlenkolonne = zahlenkolonne + " 7";
       random = (int) (Math.random() * 3)+1;
       if (random == 1){
            zwei();
        }
        if (random == 2){
            ende();
        }
        if (random == 3){
            dreizehn();
        }
    }
    public void acht(){
       count++;
       if(acht){
           start();
       }
       else{
           acht = true;
       }
       zahlenkolonne = zahlenkolonne + " 8";
       random = (int) (Math.random() * 5)+1;
       if (random == 1){
            vierzehn();
        }
        if (random == 2){
            drei();
        }
        if (random == 3){
            neun();
        }
        if (random == 4){
            vier();
        }
        if (random == 5){
            fünfzehn();
        }
    }
    public void neun(){
        count++;
        if(neun){
            start();
        }
        else{
            neun = true;
        }
       zahlenkolonne = zahlenkolonne + " 9";
       random = (int) (Math.random() * 3)+1;
       if (random == 1){
            zwei();
        }
        if (random == 2){
            acht();
        }
        if (random == 3){
            fünf();
        }
    }
    public void zehn(){
       count++;
       if(zehn){
           start();
       }
       else{
           zehn = true;
       }
       zahlenkolonne = zahlenkolonne + " 10";
       random = (int) (Math.random() * 3)+1;
       if (random == 1){
            fünfzehn();
       }
       if (random == 2){
           vier();
       }
       if (random == 3){
           neunzehn();
       }
    }
    public void elf(){
        count++;
       if(elf){
           start();
       } 
       else{
           elf = true;
       }
       zahlenkolonne = zahlenkolonne + " 11";
       random = (int) (Math.random() * 2)+1;
       if (random == 1){
            fünf();
       }
       if (random == 2){
           sechszehn();
       }
    }
    public void zwölf(){
        count++;
        if(zwölf){
            start();
        }
        else{
            zwölf = true;
        }
       zahlenkolonne = zahlenkolonne + " 12";
       random = (int) (Math.random() * 2)+1;
       if (random == 1){
            ende();
       }
       if (random == 2){
           sechs();
       }
    }
    public void dreizehn(){
       count++;
       if(dreizehn){
           start();
       } 
       else{
           dreizehn = true;
       }
       zahlenkolonne = zahlenkolonne + " 13";
       random = (int) (Math.random() * 4)+1;
       if (random == 1){
            ende();
        }
        if (random == 2){
            sieben();
        }
        if (random == 3){
            drei();
        }
        if (random == 4){
            siebzehn();
        }
       
    }
    public void vierzehn(){
        count++;
        if(vierzehn){
            start();
        }
        else{
            vierzehn = true;
        }
        zahlenkolonne = zahlenkolonne + " 14";
       random = (int) (Math.random() * 5)+1;
       if (random == 1){
            siebzehn();
        }
        if (random == 2){
            achtzehn();
        }
        if (random == 3){
            fünfzehn();
        }
        if (random == 4){
            acht();
        }
       if (random == 5){
           drei();
       }
    }
    public void fünfzehn(){
        count++;
        if(fünfzehn){
            start();
        }
        else{
           fünfzehn = true ;
        }
        zahlenkolonne = zahlenkolonne + " 15";
        random = (int) (Math.random() * 4)+1;
        if (random == 1){
            vierzehn();
        }
        if (random == 2){
            acht();
        }
        if (random == 3){
            zehn();
        }
        if (random == 4){
            zweiundzwanzig();
        }
    }
    public void sechszehn(){
        count++;
        if(sechszehn){
            start();
        }
        else{
            sechszehn = true;
        }
        zahlenkolonne = zahlenkolonne + " 16";
        random = (int) (Math.random() * 4)+1;
        if (random == 1){
            vier();
        }
        if (random == 2){
            fünf();
        }
        if (random == 3){
            elf();
        }
        if (random == 4){
            neunzehn();
        }
    }
    public void siebzehn (){
        count++;
        if(siebzehn){
            start();
        }
        else{
            siebzehn = true;
        }
        zahlenkolonne = zahlenkolonne + " 17";
        random = (int) (Math.random() * 4)+1;
        if (random == 1){
            dreizehn();
        }
        if (random == 2){
            vierzehn();
        }
        if (random == 3){
            einundzwanzig();
        }
        if (random == 4){
            sechs();
        }
    }
    public void achtzehn(){
        count++;
        if(achtzehn){
            start();
        }
        else{
            achtzehn = true;
        }
        zahlenkolonne = zahlenkolonne + " 18";
        random = (int) (Math.random() * 2)+1;
        if (random == 1){
            vierzehn();
        }
        if (random == 2){
            zweiundzwanzig();
        }
    }
    public void neunzehn (){
        count++;
        if(neunzehn){
            start();
        }
        else{
            neunzehn = true;
        }
        zahlenkolonne = zahlenkolonne + " 19";
        random = (int) (Math.random() * 2)+1;
        if (random == 1){
            zehn();
        }
        if (random == 2){
            sechszehn();
        }
    }
    public void zwanzig() {
        count++;
        if(zwanzig){
            start();
        }
        else{
            zwanzig = true;
        }
        zahlenkolonne = zahlenkolonne + " 20";
        random = (int) (Math.random() * 3)+1;
        if (random == 1){
            ende();
        }
        if (random == 2){
            sechs();
        }
        if (random == 3){
            einundzwanzig();
        }
    }
    public void einundzwanzig(){
        count++;
        if(einundzwanzig){
            start();
        }
        else{
            einundzwanzig = true;
        }
        zahlenkolonne = zahlenkolonne + " 21";
        random = (int) (Math.random() * 3)+1;
        if (random == 1){
            zwanzig();
        }
        if (random == 2){
            siebzehn();
        }
        if (random == 3){
            zweiundzwanzig();
        }
    }
    public void zweiundzwanzig() {
        count++;
        if(zweiundzwanzig){
            start();
        }
        else{
            zweiundzwanzig = true;
        }
        zahlenkolonne = zahlenkolonne + " 22";
        random = (int) (Math.random() * 3)+1;
        if (random == 1){
            einundzwanzig();
        }
        if (random == 2){
            achtzehn();
        }
        if (random == 3){
            fünfzehn();
        }
    }
    public void ende(){
        if(count != 21){
            start();
        }
        System.out.println(zahlenkolonne);
    }
}

Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author
 */
public class Main {
    public static void main (String[] args){
        Towns t = new Towns();
        t.start();
    }
}

Fehlermeldung:
run:
Exception in thread "main" java.lang.StackOverflowError
at java.util.Random.nextDouble(Random.java:438)
at java.lang.Math.random(Math.java:716)
at Towns.fünfzehn(Towns.java:374)
at Towns.zehn(Towns.java:272)
at Towns.start(Towns.java:71)
at Towns.acht(Towns.java:218)
at Towns.neun(Towns.java:255)
at Towns.acht(Towns.java:232)
at Towns.fünfzehn(Towns.java:379)
at Towns.zehn(Towns.java:272)
at Towns.start(Towns.java:71)
at Towns.ende(Towns.java:530)
at Towns.zwei(Towns.java:88)
at Towns.fünf(Towns.java:160)
at Towns.sechszehn(Towns.java:402)
at Towns.neunzehn(Towns.java:465)
at Towns.zehn(Towns.java:278)
at Towns.fünfzehn(Towns.java:382)
at Towns.zweiundzwanzig(Towns.java:525)
at Towns.start(Towns.java:65)
at Towns.sieben(Towns.java:198)
at Towns.dreizehn(Towns.java:329)
at Towns.sieben(Towns.java:212)
at Towns.zwei(Towns.java:100)
at Towns.fünf(Towns.java:160)
at Towns.elf(Towns.java:292)
at Towns.sechszehn(Towns.java:405)
at Towns.start(Towns.java:74)
at Towns.fünf(Towns.java:152)
at Towns.neun(Towns.java:258)
at Towns.fünf(Towns.java:163)
at Towns.vier(Towns.java:146)
at Towns.zehn(Towns.java:275)
at Towns.fünfzehn(Towns.java:382)
at Towns.start(Towns.java:68)
at Towns.fünf(Towns.java:152)
at Towns.neun(Towns.java:258)
at Towns.fünf(Towns.java:163)
at Towns.vier(Towns.java:146)
at Towns.sechszehn(Towns.java:399)
at Towns.neunzehn(Towns.java:465)
at Towns.zehn(Towns.java:278)
at Towns.start(Towns.java:71)
at Towns.fünfzehn(Towns.java:368)
at Towns.zweiundzwanzig(Towns.java:525)
at Towns.fünfzehn(Towns.java:385)
at Towns.start(Towns.java:68)
at Towns.fünfzehn(Towns.java:368)
...... geht endlos weiter ....
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
 
G

Gast2

Gast
Der Error wird durch zuviele Aufrufe (also sehr wahrscheinlich ne Endlosschleife) ausgelöst.
Teste das ganze erst in nem kleineren Rahmen (weniger Städte) oder lass dir schritt für schritt ausgeben was passiert und evaluier das dann.
 

Marco13

Top Contributor
Mein Hinweis wäre: Mach' es anders (mehr kann man dazu kaum sagen...). Was soll das den werden? Wenn du irgendwann mal hundert Städte hast, gehen die Methoden dann bis "hundert()"? :autsch:
 

Marco13

Top Contributor
Ein Ansatz wäre, diesen Teil hier zu nehmen
Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
und dort... neuen Quellcode drunterzuschreiben.

:joke:

Nee im ernst: Was wird das bisher gemacht? Der Overflow kommt vermutlich daher, dass du einen Zyklus hast, aber ... das Programm hat ja an sich erstmal keinen Sinn. Was genau soll damit erreicht werden...?
 
B

BOBOHelp

Gast
hallo,

im prinzip hast du eine rekursion, die stack-overflow verursach. eine schnelle lösung (nicht zu emfehlen) nur zur ausprobieren: bei start jvm stack-size zu vergrössern. sonst bleibt dich nichts anders als redesign!
 

Marco13

Top Contributor
Abhängig von der Frage, was du eingangs mit "vereinfacht" meintest, solltest du schon überlegen, ob du die Städte nicht als Knoten eines Graphen repräsentierst....

Java:
class City
{
    public String getName() { ... }
    public List<Road> getOutgoingRoads() { ... }
}

class Road
{
    public City getStartCity() { ... }
    public City getEndCity() { ... }
}

..

(NUR sinngemäß! Allein für eine vernünftige Graph-Klasse kann man viel Zeit investieren ... ggf. gleich mal sowas wie Welcome to JGraphT - a free Java Graph Library oder JUNG - Java Universal Network/Graph Framework ansehen... )
 

Ähnliche Java Themen

Neue Themen


Oben