Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot access class attributes

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    Population assemblage = new Population(1);

    I then try to access the attributes of one of those Individuals to test if the program works with:
    // 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



    public class Main {
     
      public static void main(String[] args) {
     
      Population assemblage = new Population(1);
     
        // DEBUG!!!!
        System.out.println("positionX: " + assemblage.robots[0].getPositionX());
        // DEBUG!!!!
     
      }
     
    }



    public class Population {
     
      // attributes
      Individual[] robots;
     
      // constructor
      Population(int populationSize) {
        // create a population of random individuals
        this.robots = new Individual[populationSize];
      }
     
    }



    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; }
     
    }
    Last edited by Cyburg; August 29th, 2010 at 05:22 AM.


  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot access class attributes

    I had stupidly neglected to create array members, that is why the function return null pointer error.

    Cheers,
    Paul

Similar Threads

  1. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  2. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  3. access restriction??? wtf!?!
    By wolfgar in forum Java Theory & Questions
    Replies: 6
    Last Post: November 23rd, 2009, 07:27 AM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM
  5. Access the Same Socket
    By ahmmnhwa in forum Java Networking
    Replies: 5
    Last Post: October 29th, 2009, 11:50 AM