0

I'm trying to program a genetic algorithm in JAVA that solve the "travelling salesman problem ". I have 5 classes :

  1. "Ville" (a description of a town : longitute, latitude, name)
  2. "GestionnaireCircuit" (a class to manipulate a Ville array)
  3. "Circuit" (a possible solution of the problem; a certain order of arranging the towns)
  4. "Population" (several Circuit)
  5. "GA" (the genetic algorithm stuff to perfom the solution)

My problem is that I have this error : "java.lang.NullPointerException" because of bad initialization of "Circuit"

here's my code :

public class Circuit {

    GestionnaireCircuit gc ; 
    Ville circuit []; 
    double fitness ; 
    double distance ; 

    public Circuit(GestionnaireCircuit g){
        gc = g ; 
        circuit = new Ville [g.nombreVilles()]; //the error is here, probably the use of "g"
        for (int i=0;i<circuit.length;i++){
            circuit[i] = null ; 
        }
        fitness = 0 ;
        distance = 0;
    }
}

If you want to see the GestionnaireCircuit class, here is it :

public class GestionnaireCircuit {

    Ville villesDestinations [] ; 

    public GestionnaireCircuit(Ville []gc){
        villesDestinations = new Ville [gc.length] ; 
        for (int i=0;i<gc.length;i++){
            villesDestinations[i] = gc[i] ; 
        }
    }

    public void ajouterVille(Ville a){
        villesDestinations = new Ville[villesDestinations.length + 1] ; 
        villesDestinations[villesDestinations.length -1]=a ; 
    }

    public Ville getVille(int index){
        Ville a = villesDestinations[index] ; 
        return a ; 
    }

    public int nombreVilles(){
        return villesDestinations.length;
    }
}

The Population class :

public class Population {

GestionnaireCircuit gc ; 
Circuit circuits[] ; 
int taillePopulation ; 
Boolean init ; 

public Population(GestionnaireCircuit g, int taillepopu, Boolean init1){
    circuits = new Circuit[taillepopu] ;
    taillePopulation = taillepopu ; 
    init = init1 ; 
    if (init1 == true){
        for (int i =0; i<taillepopu;i++){
        Circuit nouveauCircuit = new Circuit(gc);
        nouveauCircuit.genererIndividu();
        circuits[i] = nouveauCircuit ; 
        }
    }

}

And then the NewMain :

public static void main(String[] args) {
    // TODO code application logic here

    Ville ville1 = new Ville(3.002556, 45.846117, "Clermont-Ferran");

    Ville ville2 = new Ville(-0.644905, 44.896839, "Bordeaux");

    Ville ville3 = new Ville(-1.380989, 43.470961, "Bayonne");

    Ville ville4 = new Ville(1.376579, 43.662010, "Toulouse");

    Ville ville5 = new Ville(5.337151, 43.327276, "Marseille");

    Ville ville6 = new Ville(7.265252, 43.745404, "Nice");

    Ville ville7 = new Ville(-1.650154, 47.385427, "Nantes");

    Ville ville8 = new Ville(-1.430427, 48.197310, "Rennes");

    Ville ville9 = new Ville(2.414787, 48.953260, "Paris");

    Ville ville10 = new Ville(3.090447, 50.612962, "Lille");

    Ville ville11 = new Ville(5.013054, 47.370547, "Dijon");

    Ville ville12 = new Ville(4.793327, 44.990153, "Valence");

    Ville ville13 = new Ville(2.447746, 44.966838, "Aurillac");

    Ville ville14 = new Ville(1.750115, 47.980822, "Orleans");

    Ville ville15 = new Ville(4.134148, 49.323421, "Reims");

    Ville ville16 = new Ville(7.506950, 48.580332, "Strasbourg");

    Ville ville17 = new Ville(1.233757, 45.865246, "Limoges");

    Ville ville18 = new Ville(4.047255,48.370925, "Troyes");

    Ville ville19 = new Ville(0.103163,49.532415, "Le Havre");

    Ville ville20 = new Ville(-1.495348, 49.667704, "Cherbourg");

    Ville ville21 = new Ville(-4.494615, 48.447500, "Brest");

    Ville ville22 = new Ville(-0.457140, 46.373545, "Niort");

    Ville tab[] = {ville1,ville2,ville3,ville4,ville5,ville6,ville7,ville8,ville9,
                  ville10,ville11,ville12,ville13,ville14,ville15,ville16,ville17,
                  ville18,ville19,ville20,ville21,ville22} ; 

    GestionnaireCircuit gc = new GestionnaireCircuit(tab) ;



    Population pop = new Population (gc,50,true) ;
    System.out.println("La Distance du meilleur circuit sur la première propulation est de:" + pop.getFittest().getDistance());
    GA ga = new GA(gc);
    pop = ga.evoluerPopulation(pop);
    for (int i=0;i<100;i++){
        pop = ga.evoluerPopulation(pop); 
        System.out.println("la meilleur distance pour la population" + (i+2) + "est : " + pop.getFittest().getDistance());
    }
    System.out.println("Après 100 générations, la meilleure distance est : " + pop.getFittest().getDistance());
}

And here's the error : Exception in thread "main" java.lang.NullPointerException at Circuit.<init>(Circuit.java:22) at Population.<init>(Population.java:24) at NewMain.main(NewMain.java:71)

I think that comes from a polymorphic problem, because I use several empty class.

Do you have an idea to solve this issue ?

Thank you very much

brash6
  • 1
  • 1
  • 2
    Please always include the full stack trace of an exception when one is thrown. – Turing85 Apr 14 '18 at 16:50
  • Exception in thread "main" java.lang.NullPointerException at Circuit.(Circuit.java:22) at Population.(Population.java:24) at NewMain.main(NewMain.java:71) – brash6 Apr 14 '18 at 17:05
  • 2
    Please [edit] your question and add this information instead of posting it as comment. – Turing85 Apr 14 '18 at 17:06
  • Also include NewMain.java since that's what is throwing the exception. – Always Learning Apr 14 '18 at 17:11
  • `GestionnaireCircuit gc ;` is declared in your `Population` class, but you don't set it to anything before you pass it to the `Circuit` constructor on the line `Circuit nouveauCircuit = new Circuit(gc);`. At that point in your code, `gc` is just a reference that doesn't point to any object. When you try to call a method on that reference inside the `Circuit` constructor, you get the error. – Bill the Lizard Apr 14 '18 at 18:51
  • Thank you so much, I'm on it since yesterday and the code juste compiled. You've juste made my day !!! – brash6 Apr 14 '18 at 18:56
  • What's funny: `at Circuit.(Circuit.java:22)`, but Circuit.java only has 17 lines of code. If you sanitze your code maybe from company name in header - recompile and rerun it to get valid output. Setting `circuit[i] = null ;` to null for the whole, freshly created array seems senseless to me too. – user unknown Apr 14 '18 at 21:40

0 Answers0