Data not being read from file

Sorry to be asking you guys so many questions but I guess with this project I bit off a bit more than I could chew...

I'm trying to read data from a dat file and then putting it into an array to display. However it just won't read it into the array for some reason. I'd really appreciate any help!

Here is the method which calls for the array to be made:

publicboolean checkForFile(){

try{

FileReader theFile =new FileReader("HeroDatabase.dat");

BufferedReader inFile =new BufferedReader(theFile);

Hero root =new Hero();

LoadSave temp =new LoadSave(root);

int records = temp.countRecords();

Hero[] tempArray =new Hero[records];

DotaDatabase app =new DotaDatabase();

tempArray = app.retrieveDatabase();

heroList.setListData(tempArray);

inFile.close();

JetsIO.output("The file has been loaded for viewing.");

returntrue;

}

catch(Exception io){

heroDetails.setText("Error trying to open file " + io.getMessage());

HeroViewer.this.setVisible(false );

HeroViewer.this.dispose();

GUI menu =new GUI();//helper method

}

returnfalse;

}

And this is the method which actually creates the array:

public Hero[] retrieveDatabase(){

Hero root =new Hero();

LoadSave temp =new LoadSave(root);

Hero[] tempArray =new Hero[temp.countRecords()];

for (int x = 0; x < temp.countRecords(); x++){

String name = temp.readName(x);

String heroClass = temp.readHeroClass(x);

String primaryAttribute = temp.readPrimaryAttribute(x);

String heroType = temp.readHeroType(x);

String affiliation = temp.readAffiliation(x);

int armour = temp.readArmour(x);

int strength = temp.readStrength(x);

int agility = temp.readAgility(x);

int intelligence = temp.readIntelligence(x);

double strengthGainPerLevel = temp.readStrengthGainPerLevel(x);

double agilityGainPerLevel = temp.readAgilityGainPerLevel(x);

double intelligenceGainPerLevel = temp.readIntelligenceGainPerLevel(x);

int damage = temp.readDamage(x);

int attackRange = temp.readAttackRange(x);

String skill1 = temp.readSkill1(x);

String skill2 = temp.readSkill2(x);

String skill3 = temp.readSkill3(x);

String ultimateSkill = temp.readUltimateSkill(x);

String description = temp.readDescription(x);

Hero toBeLoaded =new Hero();

toBeLoaded.setName(name);

toBeLoaded.setHeroClass(heroClass);

toBeLoaded.setPrimaryAttribute(primaryAttribute);

toBeLoaded.setType(heroType);

toBeLoaded.setAffiliation(affiliation);

toBeLoaded.setArmour(armour);

toBeLoaded.setStrength(strength);

toBeLoaded.setAgility(agility);

toBeLoaded.setIntelligence(intelligence);

toBeLoaded.setStrengthGainPerLevel(strengthGainPerLevel);

toBeLoaded.setAgilityGainPerLevel(agilityGainPerLevel);

toBeLoaded.setIntelligenceGainPerLevel(intelligenceGainPerLevel);

toBeLoaded.setDamage(damage);

toBeLoaded.setAttackRange(attackRange);

toBeLoaded.setSkill1(skill1);

toBeLoaded.setSkill2(skill2);

toBeLoaded.setSkill3(skill3);

toBeLoaded.setUltimateSkill(ultimateSkill);

toBeLoaded.setDescription(description);

tempArray[x] = toBeLoaded;

}

return tempArray;

}

I don't know why it won't display the array, it appears to have loaded the file and entered it into an array but just can't display the array.

[5051 byte] By [NochnoiDozora] at [2007-10-3 4:08:35]
# 1

Gah, it won't let me update my above post, but here is the code with a couple of additions, but still the same problem:

public boolean checkForFile(){

try{

FileReader theFile = new FileReader("HeroDatabase.dat");

BufferedReader inFile = new BufferedReader(theFile);

Hero root = new Hero();

LoadSave temp = new LoadSave(root);

int records = temp.countRecords();

Hero[] tempArray = new Hero[records];

DotaDatabase app = new DotaDatabase();

tempArray = app.retrieveDatabase();

heroList.setListData(tempArray);

inFile.close();

JetsIO.output("The file has been loaded successfully.");

return true;

}

catch(Exception io){

heroDetails.setText("Error trying to open file " + io.getMessage());

HeroViewer.this.setVisible( false );

HeroViewer.this.dispose();

}

return false;

}

/**

* retrieveDatabase method to get the data from file on disk and insert it into the binary tree in RAM

* Utilises FOR loops

* Utilises binary selection in IF loops

* @return Hero[] tempArray, array of Hero objects for display

*/

public Hero[] retrieveDatabase(){

Hero root = new Hero();

Hero toBeLoaded = new Hero();

LoadSave temp = new LoadSave(root);

int records = temp.countRecords();

Hero[] tempArray = new Hero[records];//array declaration for storing of records

for (int x = 0; x < records; x++){

boolean deleted = temp.readDeleted(x);

if (deleted = false){//checking whether the record has been flagged as deleted (ignored)

//reading attribute data from the file into variables

String name = temp.readName(x);

String heroClass = temp.readHeroClass(x);

String primaryAttribute = temp.readPrimaryAttribute(x);

String heroType = temp.readHeroType(x);

String affiliation = temp.readAffiliation(x);

int armour = temp.readArmour(x);

int strength = temp.readStrength(x);

int agility = temp.readAgility(x);

int intelligence = temp.readIntelligence(x);

double strengthGainPerLevel = temp.readStrengthGainPerLevel(x);

double agilityGainPerLevel = temp.readAgilityGainPerLevel(x);

double intelligenceGainPerLevel = temp.readIntelligenceGainPerLevel(x);

int damage = temp.readDamage(x);

int attackRange = temp.readAttackRange(x);

String skill1 = temp.readSkill1(x);

String skill2 = temp.readSkill2(x);

String skill3 = temp.readSkill3(x);

String ultimateSkill = temp.readUltimateSkill(x);

String description = temp.readDescription(x);

//putting the data into a Hero object for ease of transportation

toBeLoaded.setName(name);

toBeLoaded.setHeroClass(heroClass);

toBeLoaded.setPrimaryAttribute(primaryAttribute);

toBeLoaded.setType(heroType);

toBeLoaded.setAffiliation(affiliation);

toBeLoaded.setArmour(armour);

toBeLoaded.setStrength(strength);

toBeLoaded.setAgility(agility);

toBeLoaded.setIntelligence(intelligence);

toBeLoaded.setStrengthGainPerLevel(strengthGainPerLevel);

toBeLoaded.setAgilityGainPerLevel(agilityGainPerLevel);

toBeLoaded.setIntelligenceGainPerLevel(intelligenceGainPerLevel);

toBeLoaded.setDamage(damage);

toBeLoaded.setAttackRange(attackRange);

toBeLoaded.setSkill1(skill1);

toBeLoaded.setSkill2(skill2);

toBeLoaded.setSkill3(skill3);

toBeLoaded.setUltimateSkill(ultimateSkill);

toBeLoaded.setDescription(description);

toBeLoaded.setDeleted(deleted);

}//end if

tempArray[x] = toBeLoaded;//add a Hero object to the array

}//end for

return tempArray;//return the array full of Heroes

}//end retrieveDatabase method

NochnoiDozora at 2007-7-14 22:08:25 > top of Java-index,Java Essentials,New To Java...
# 2

...

> Hero toBeLoaded = new Hero();

...

> for (int x = 0; x < records; x++){

> tempArray[x] = toBeLoaded;//add a Hero object to

> }//end for

...

Well, think of what you're doing.

You're initalizing an array of References. Then you initalize ONE Hero object. you fill that object with data. Then you set one of the array references to point to that Hero object. Then you change the Hero object and go over it again. But remember - the reference still points to the same Hero object! So essentially, you're filling the array with one Hero object with data matching the last successful iteration.

While I don't know your code, my wild guess is that you need to to:

for(...){

toBeLoaded = new Hero();

...

array[x] = toBeLoaded;

} //for

Please read some on References and Objects.

Zaph0da at 2007-7-14 22:08:25 > top of Java-index,Java Essentials,New To Java...
# 3
Hmm, I understand you logic - but I fixed up the code as you suggested and it still doesn't work proerly.
NochnoiDozora at 2007-7-14 22:08:25 > top of Java-index,Java Essentials,New To Java...
# 4
well, post some more data - what exceptions are you getting? what's the result of the process? what's the expected result?
Zaph0da at 2007-7-14 22:08:25 > top of Java-index,Java Essentials,New To Java...
# 5
I'm not getting any exceptions at all. As soon as it is supposed to add the data to the array and display it, it just doesn't do it. The list is still blank even though the array has been added to it.
NochnoiDozora at 2007-7-14 22:08:25 > top of Java-index,Java Essentials,New To Java...