FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Arrays of objects, which are arrays of objects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Arrays of objects, which are arrays of objects  (Read 330 times)
jhv2

WWW
Arrays of objects, which are arrays of objects
« on: Feb 29th, 2004, 1:15am »

I am trying to make a GA program to breed cities.  The thing is going to be awesome, except it doesn't work.
 
I have a class of one "Population", which makes an array of 15 "Cities".
 
I have that class "City" make an array of 48 "Parcels."
 
Parcels are a class, which makes its buildings according to a genetic code I reference in text files.
 
I keep getting the NullPointerException, it appears, because Processing won't dig into that second array of objects.
 
Before I go nuts re-writing this to only deal with one HUGE array of objects, does anybody know if I'm doing something illegal, nesting objects in the code posted here...
 
http://pantheon.yale.edu/~jhv2/SimonClearingHouse/CheckItOut.htm
 
(thanks),
 
Jason  
 
« Last Edit: Feb 29th, 2004, 4:56pm by jhv2 »  
TomC

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #1 on: Feb 29th, 2004, 7:55pm »

OK, I got this far...
 
Code:

class Population{
 
int numCities = 15;
City cities[];
 
void makeCities(){
cities = new City[numCities];
for(int n=0; n<numCities; n++){
cities[n].initializeParcels();
}
}
 
void drawCity(int n){
cities[n].drawParcels();
}
}
 

 
In the class above, you aren't initialising each city.  You need to create a new City at each point in your array, so add this inside the loop in your makeCities function:
 
Code:

  cities[n] = new City();

 
You might have made that mistake in other places too, but I stopped looking there.  Hope that points you in the right direction.
« Last Edit: Feb 29th, 2004, 8:05pm by TomC »  
jhv2

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #2 on: Feb 29th, 2004, 10:36pm »

I made those changes and am still getting the same error.  I'm wondering if the definitions of classes cannot contain objects of other custom classes.  i.e. the example below doesn't run either...
 
<code>
 
ArchProfessor Sim;
BFont arial;  
 
void setup(){
size(300,300);
arial = loadFont("Arial.vlw");
textFont( arial, 20 );
 
Sim = new ArchProfessor();
Sim.says();
}
 
void loop(){
background(0);
fill(255);
text( Sim.typeStuff(1,3), frame, 50 );
}
 
 
class ArchProfessor {
Stuff things[] = new Stuff[5];
 
void says(){
for( int j=0; j<5; j++ ){
things[j].putOn();
}
}
 
int typeStuff(int i, int j){
return things[i].numbers[j];
}
}
 
 
class Stuff {
int numbers[] = new int[7];
 
void putOn(){
for( int i=0; i<7; i++ ){
numbers[i] = i;
}
}
}
 
</code>
 
is this where we're trying to instantiate an egg, but the compilers needs an instance of the chicken first?
 
TomC

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #3 on: Feb 29th, 2004, 10:50pm »

Nope, not a chicken/egg problem, but an non-initialised object problem.
 
Same thing again, you haven't instantiated the elements in your Stuff array this time...
 
Basically, when you create an array...
 
Code:

// declare things as an array of Stuffs,  
// and instantiate it as a new array of 5 stuffs  
Stuff things[] = new Stuff[5];  

 
...it's like asking for a box with 5 compartments, each big enough for a Stuff.  
 
But you still need to decide what Stuff goes in what compartment, the most basic way being to make a new Stuff for each compartment, like so:
 
Code:

for( int i=0; i<5; i++ ){  
  // instantiate the i'th thing as a new Stuff
  things[i] = new Stuff();
  // now a method call won't cause an error
  things[i].putOn();  
}  

 
Hope that makes things clearer.
 
jhv2

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #4 on: Mar 1st, 2004, 4:10am »

TomC, I really appreciate you taking the time to look at my code.  You really are patient and nice too put up with this nasty little problem....  Thank you.
 
but...
 
I think the problem is a little different.  My teacher uses the technique you're correcting here...
 
http://www.architecture.yale.edu/873b/lectures/7/12.html
 
 
...and it still works.
 
I've made those corrections.  I usually have no problem making objects of any class.  The difficulty  arises when I try to instantiate objects which contain objects of another class.
 
http://pantheon.yale.edu/~jhv2/SimonClearingHouse/CheckItOut.htm
 
Do you know what I mean?  Can you show me an example of a program that does what I'm looking for, `cause I don't know where to look.
 
(Thanks again for your time)
 
Jason
 
 
TomC

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #5 on: Mar 1st, 2004, 9:02am »

OK, we might be missing each other's points here, but the code you just pointed to has the following line
 
Code:

walkers[i] = new Walker();

 
That means it's assigning each element in the walkers array to be a new Walker.  Before it does that, walkers[i].init() would cause an error.  All of the examples of your code ommit a line of that kind somewhere or other.  Make sure you are initialising every element, or you will get a NullPointerException.
 
I'll post an example later.
 
Keep going!
 
TomC

WWW
Re: Arrays of objects, which are arrays of objects
« Reply #6 on: Mar 1st, 2004, 10:33am »

Code:

 
 
int NUM_MEDIUMS = 10;
int NUM_SMALLS  = 10;
 
Big big;
 
void setup() {
  size(400,400);
  big = new Big();
  big.init();
  background(100,20,40);
}
 
void loop() {
  big.draw();
  big.move();
}
 
void mouseClicked() {
  background(100,20,40);
  big.init();
}
 
///////////////////////////////////////////
 
class Big {
   
  Medium[] mediums;
 
  // constructor, instantiates things which must be instantiated before use
  Big() {
    // instantiate array
    mediums = new Medium[NUM_MEDIUMS];
    // set each element of the array to be a new Medium()
    for (int i = 0; i < mediums.length; i++) {
 mediums[i] = new Medium();
    }  
  }
 
  // init method just inits each element of mediums
  void init() {
    for (int i = 0; i < mediums.length; i++) {
 mediums[i].init();
    }  
  }
 
  // draw method just draws each element of mediums
  void draw() {
    for (int i = 0; i < mediums.length; i++) {
 mediums[i].draw();
    }
  }
 
  // move method just moves each element of mediums
  void move() {
    for (int i = 0; i < mediums.length; i++) {
 mediums[i].move();
    }
  }
   
}
 
class Medium {
 
  color col;  
  Small[] smalls;
   
  Medium() {
    smalls = new Small[NUM_SMALLS];
    for (int i = 0; i < smalls.length; i++) {
 smalls[i] = new Small();
    }
    col = 0;
  }
   
  void init() {
    col = color(random(200,255),random(128,200),random(200,255));
    for (int i = 0; i < smalls.length; i++) {
 smalls[i].init();
    }
  }
   
  void draw() {
    stroke(col);
    for (int i = 0; i < smalls.length; i++) {
 smalls[i].draw();
    }
  }
   
  void move() {
    float averageX = 0;
    float averageY = 0;
    for (int i = 0; i < smalls.length; i++) {
 averageX += smalls[i].x;
 averageY += smalls[i].y;
    }
    averageX /= smalls.length;
    averageY /= smalls.length;
    for (int i = 0; i < smalls.length; i++) {
 smalls[i].x += (averageX - smalls[i].x) / 50;
 smalls[i].y += (averageY - smalls[i].y) / 50;
    }
  }
   
}
 
class Small {
   
  float x,y;
   
  Small() {
    x = 0.0;
    y = 0.0;
  }
   
  void init() {
    x = random(0,width-1);
    y = random(0,width-1);
  }
   
  void draw() {
    point(x,y);
  }
   
}
 
 
« Last Edit: Mar 1st, 2004, 10:38am by TomC »  
Jason.vannest@Yale.edu
Guest
Email
Re: Arrays of objects, which are arrays of objects
« Reply #7 on: Mar 1st, 2004, 8:24pm »

Dear TomC,
 
I spoke with my professor just now, and just returned to see your posting.
 
YOU ARE THE BEST!
 
I apologize for not seeing your point sooner, cause I must have been implimenting your suggestions wrong (more than once!).
 
Thank you again, cause now I've got some work to do....
 
PEACE!
 
Jason
 
Pages: 1 

« Previous topic | Next topic »