trouble with objects containing other objects
Hi. I'm having trouble with basic objects in Java.
I have one class that contains another class that in turn contains a third class.
The highest level class looks like this:
publicclass floppydisk{
track [] trackgroup;
/** Creates a new instance of floppydisk */
public floppydisk(){
trackgroup =new track[160];
}
}
track looks like this:
publicclass track{
sector [] secgroup;
/** Creates a new instance of track */
public track(){
secgroup =new sector[11];
}
}
The third class, sector is here
publicclass sector{
int format_type;
int track;
int sector;
int offset;
int data_chk;
int [] data;
/** Creates a new instance of sector */
public sector(){
format_type = 0;
track = 0;
sector = 0;
offset = 0;
data_chk = 0;
data =newint[512];
}
}
I call it from the main routine like this:
floppydisk myfloppy;
myfloppy =new floppydisk();
While the constructor for floppydisk() executes, the underlying track() constructor is not called, and neither is the sector() constructor. Because they aren't being called, I get an null pointer exception, I'm guessing because the appropriate fields aren't being initialized and memory allocated.
I'm trying to access the fields like this:
myfloppy.trackgroup[0].secgroup[0].format_type = 0xFF;
Do I need to manually loop through the tracks and sectors and create each
new instance of each object separately? If so, can someone please post a code snippet showing the best way to achieve this?
Most of my experience is in C (structures work easily in this fashion), with some in C++.
Thanks
Keith

