Melo1991
Melo1991

Reputation: 375

java.lang.NullPointerException within a GA

I'm doing my Final year project using a Genetic Algorithm to design a set of time tables for my college.

I'm running into 1 error that is stopping me from moving forward. I'm getting the following errors.

 Exception in thread "main" java.lang.NullPointerException
at LunchTimeFree.calculateViolations(LunchTimeFree.java:36)
at FitnessFunction.calculateFitnesScore(FitnessFunction.java:34)
at Main.main(Main.java:19)

This isn't making any sense to me though: At that line is this line of code:

    int time = c.genes[i].time;

It's set the variable time to the time value of a certain gene within my chromosome. I've debugged it and inserted a println statement after printing my time variable and the c.genes[i].time variable and they are both initialized to variables, i.e time = 1 or 2 etc.

I don't understand what the problem is. Please can someone help me?!

public class LunchTimeFree extends constraintInterface {

    Chromosome c;

    public LunchTimeFree(Chromosome chromo){
        c = chromo;
    }

    @Override
    public double calculateViolations() {     
    double violations=0;    

    int i = 0; 
    int y = c.getLength();

    while(i <= y-1 )
    {
        int time = c.genes[i].time;

        if (time >= 16 && time <= 25)   {                           
            violations = violations + 1;        
                            } 
                i++;
    }      

    return violations*this.weight;

    }
}

public class ChromosomeFactory
{

Chromosome c;

 public ChromosomeFactory(int l)
 {

 c = new Chromosome(l);

 DummyDatabase da = new DummyDatabase();

 int numOfLectures = da.GetNumLectures();

  int i=0;

  while(i<=numOfLectures-1)
   {

   int lecture = da.getLecture(i);   

   int r = da.Rooms[(int)(Math.random() * da.Rooms.length)];
   int t = da.TimeSlots[(int)(Math.random() * da.TimeSlots.length)]; 

   Gene g = new Gene(lecture,lecture,r,t);

    c.genes[i] = g;
           System.out.println(""+this.c.genes[i].teacher+","+this.c.genes[i].lecture+","+this.c.genes[i].room+","+this.c.genes[i].time+"");
    i++;
    }
   }  
   }

 public class FitnessFunction 
{
Chromosome c;

public FitnessFunction(Chromosome newChromo)  {
    c = newChromo;
}

public double calculateFitnesScore() {

    LunchTimeFree ltf = new LunchTimeFree(c);

    ltf.setWeight(0.01);

    double violationScore =ltf.calculateViolations();

    double score = (1/(1 + violationScore));

    return score;

  } 

 public class Chromosome 
 {

Gene[] genes;



public Chromosome(int l)
{
genes = new Gene[l]; 

}

public int getLength()
{
return genes.length;
}



}

public class Gene 
{
public int teacher;
public int lecture;
public int room;
public int time;


public Gene(int t,int l, int r, int time)
{

this.teacher=t;    
this.lecture=l;
this.room=r;
this.time=time;

}



}

public class DummyDatabase implements DatabaseAccessor
{

 int[] Lecturers = { 1,2,3,4,5,6,7,8,9 };
  int[] Lectures = { 1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19} ;

  int[] TimeSlots =      {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55};

    int[] Rooms = {1,2,3,4,5,6,7,8,9,10};





@Override
public int GetRoom(int entry) {

    return this.Rooms[entry];
}

@Override
public int GetNumLectures() {
    return this.Lectures.length;
}

@Override
public int getTeacher(int entry) {
    return this.Lecturers[entry];
}

@Override
public int getLecture(int entry) {
    return this.Lectures[entry];
}

@Override
public int getNumRooms() {
    return this.Rooms.length;
}

}

Upvotes: 0

Views: 197

Answers (2)

Pyranja
Pyranja

Reputation: 3589

First of all: Splitting

int time = c.genes[i].time;

into

final Gene[] allGenes = c.genes;
final Gene aGene = allGenes[i];
final int time = aGene.time;

should show you exactly which of the references is null in the stacktrace. That may help to narrow down the location of the bug.

Apart from that maybe a long call, but all else in your posted code looks ok:

Does getLength in your Chromosome class return the length of the genes array? Or the number of set genes in the array? If the genes array is not fully filled and getLength indicates the (incorrect) number of filled array slots the while loop may overshoot and hit a null in the array. Note that you use the int variable l as constructor argument for a Chromosome in the ChromosomeFactory, but fill the genes array up to numOfLectures. If l is used to set the size of the gene array in Chromosome and l and numOfLectures differ you may have a problem. The difference will be filled with nulls in the array.

int y = c.getLength();
while(i <= y-1 )
{
    int time = c.genes[i].time;
    if (time >= 16 && time <= 25)   {                           
        violations = violations + 1;        
    } 
    i++;
}

Also consider replacing that with a for-loop like

for (int i = 0; i < c.getLength(); i++) {
   int time = c.genes[i].time;
   if (time >= 16 && time <= 25) {                           
      violations++;   
   }
}

For loops should be preferred, as they minimize the scope of loop variables, handle the increasing of loop variables and are easier to grasp. Also consider using one of the Collection classes instead of an array. And strongly consider changing all your instance variables to private visibility with getter/setters.

If that is not the reason you have to show us more code, e.g. the Chromoson class and how your genes array is structured.

Upvotes: 0

Udo Held
Udo Held

Reputation: 12538

Either c or one of your values in c.genes[i] is null.

You are trying to do an operation or request a property that is null. So you are getting a NullPointerException. If you are using Eclipse or some other IDE of your choice you could debug it and see whats null or post the relevant code so that we can take a guess. Maybe your loop for i is flawed.

Upvotes: 2

Related Questions