Cannot access class attributes
Hi everyone,
This problem is starting to drive me insane. I'm sure it must be something really simple, but I just can't seem to find out where the problem is. I initialize a population from the main() function, containing a collection of individuals with:
Code :
Population assemblage = new Population(1);
I then try to access the attributes of one of those Individuals to test if the program works with:
Code :
// DEBUG!!!!
System.out.println("positionX: " + assemblage.robots[0].getPositionX());
// DEBUG!!!!
But it does not work, the compiler throws a null pointer exception. Any help anyone can provide will be gratefully accepted.
Cheers,
Paul
Code :
public class Main {
public static void main(String[] args) {
Population assemblage = new Population(1);
// DEBUG!!!!
System.out.println("positionX: " + assemblage.robots[0].getPositionX());
// DEBUG!!!!
}
}
Code :
public class Population {
// attributes
Individual[] robots;
// constructor
Population(int populationSize) {
// create a population of random individuals
this.robots = new Individual[populationSize];
}
}
Code :
public class Individual {
// attributes
int[] chromosome; // robots chromosome bit string array
float fitness; // a measure of the fitness of this individual
int positionX; // x coordinate of the robot within the Donutland
int positionY; // y coordinate of the robot within the Donutland
int velocityX; // value of the robots velocity for the x axis
int velocityY; // value of the robots velocity for the y axis
// constructors
Individual() {
// the chromosome bit string is generated randomly
this.chromosome = generateRandomChromosome(10); // USING LENGTH 10 FOR NOW!!
// now fire function which decodes chromosome bit string into attributes
decodeChromosomeString();
}
Individual(int[] chromosome) {
// the chromosome bit string is passed in parameters
this.chromosome = chromosome;
// now fire function which decodes chromosome bit string into attributes
decodeChromosomeString();
}
// private functions
private void decodeChromosomeString() {
// fill attributes with decoded values from the chromosome bit string
// WRITE CODE TO GO HERE!!!!!
// DEBUG!!!!
this.positionX = 20;
this.positionY = 20;
this.velocityX = 1;
this.velocityY = 1;
// DEBUG!!!!
}
private void encodeChromosomeString() {
// write a chromosome bit string from instance attributes
// WRITE CODE TO GO HERE!!!!!
}
private int[] generateRandomChromosome(int length) {
// make a temporary array to work with of the required length
int[] tempChromosome;
tempChromosome = new int[length];
// setup a for loop in order to populate the bit string
for(int i=0; i==length; i++) {
tempChromosome[i] = (int)(Math.random()*2);
}
// now return the chromosome to the calling code
return tempChromosome;
}
// public functions
public int compareTo(Individual robot) {
if (this.fitness == ((Individual) robot).fitness)
return 0;
else if ((this.fitness) > ((Individual) robot).fitness)
return 1;
else
return -1;
}
public int compare(Individual robotA, Individual robotB) {
if (((Individual) robotA).fitness == ((Individual) robotB).fitness)
return 0;
else if (((Individual) robotA).fitness > ((Individual) robotB).fitness)
return 1;
else
return -1;
}
public int getPositionX() { return this.positionX; }
public int getPositionY() { return this.positionY; }
public int getVelocityX() { return this.velocityX; }
public int getVelocityY() { return this.velocityY; }
public int[] getChromosome() { return this.chromosome; }
public void setPositionX(int positionX) { this.positionX = positionX; }
public void setPositionY(int positionY) { this.positionY = positionY; }
public void setVelocityX(int velocityX) { this.velocityX = velocityX; }
public void setVelocityY(int velocityY) { this.velocityY = velocityY; }
public void setChromosome(int[] chromosome) { this.chromosome = chromosome; }
}
Re: Cannot access class attributes
I had stupidly neglected to create array members, that is why the function return null pointer error.
Cheers,
Paul