Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:24:12+00:00 2026-06-14T15:24:12+00:00

I’m doing my Final year project using a Genetic Algorithm to design a set

  • 0

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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:24:14+00:00Added an answer on June 14, 2026 at 3:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.