GA 中的 java.lang.NullPointerException

标签 java nullpointerexception

我正在做最后一年的项目,使用遗传算法为我的大学设计一套时间表。

我遇到了 1 个错误,该错误阻止了我继续前进。我收到以下错误。

 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)

但这对我来说没有任何意义:在那一行是这行代码:

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

它将变量时间设置为我染色体内某个基因的时间值。 我已经对其进行了调试,并在打印我的时间变量和 c.genes[i].time 变量后插入了一条 println 语句,并且它们都被初始化为变量,即 time = 1 或 2 等。

我不明白问题出在哪里。请问有人可以帮助我吗?!

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

}

最佳答案

cc.genes[i] 中的值之一为 null。

您正在尝试执行一项操作或请求一个为空的属性。所以你会得到一个NullPointerException。如果您使用的是 Eclipse 或您选择的其他 IDE,您可以对其进行调试并查看什么为 null 或发布相关代码,以便我们进行猜测。也许您的 i 循环有缺陷。

关于GA 中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326017/

相关文章:

java - 在 HTML 文件中显示汉字

java JMenuItem NullpointerException(覆盖 swing 类时初始化错误)

java - 如何将字符串转为整数并相加?

带有 CSS 的矩形上的 JavaFX 图像

java - 需要帮助分析 Java 崩溃

java - jsp、servlet连接mysql数据库

java - 相机 "Bitmap imageBitmap = (Bitmap) extras.get("数据");"给出 Nullpointer 错误

java - 如何在java中按时间对对象列表进行排序

java - HBase:MiniDFSCluster.java 在某些环境中失败

java - System.console() 在 NetBeans 中给出 NullPointerException