Нахождение целых корней диофантового уравнения(генетический алгоритм)
Пытаюсь заполнить массив public int[] alleles = new int[4]; класса Gene рандомными числами от 0 до result, но при обращении к этому массиву из функции solve() класса Diafant, выдаёт ошибку NullPointerException. Строка, где возникает исключение, помечена в коде.
Класс Gene:
public class Gene {
public int[] alleles = new int[4];
int fitness;
float likelihood;
// Test for equality.
public boolean equalOperator(Gene gene) {
for(int i = 0; i < 4; i++) {
if (gene.alleles[i] != alleles[i]) {
return false;
}
}
return true;
}
}
Класс Diofant:
public class Diofant {
final int MAXPOPULATION = 25;
int ca, cb, cc, cd; //coefficients
int result;
Gene[] population = new Gene[MAXPOPULATION]; //MaxPopulation
public Diofant(int a, int b, int c, int d, int res) {
this.ca = a;
this.cb = b;
this.cc = c;
this.cd = d;
this.result = res;
}
public Gene getGene(int i) {
return population[i];
}
int solve() {
int fitness = -1;
// Generate initial generation
for(int i = 0; i < MAXPOPULATION; i++) {
for(int j = 0; j < 4; j++) {
population[i].alleles[j] = (int) (Math.random() * (result + 1)); //ИСКЛЮЧЕНИЕ ЗДЕСЬ !!!!
}
}
if (fitness == createFitnesses()) {
return fitness;
}
int iterations = 0; // keep track of the iterations
while (fitness != 0 || iterations < 50) {
generateLikelihoods();
createNewPopulation();
if (fitness == createFitnesses()) {
return fitness;
}
iterations++;
}
return -1;
}