I have a class called ‘Items’ to which ‘Equips’ extends from and ‘Helmet’ then extends from ‘Equips’. I have a method called ‘getStats’ that loads the item’s stats from a .txt file. If I put the ‘getStats’ method in the ‘Items’ class, whatever field I try to access in a ‘Helmet’ object using ‘this.’ shows up null. The field I’m trying to access in ‘Helmet’ is initialized when the helmet is created before the text file is loaded. I could very easily just put the ‘getStats’ method in the ‘Equips’ class and put a blank ‘getStats’ method in the ‘Items’ class, but I was wondering if there was a way to make it work how it is. Thanks in advance!
Items.java:
package com.projects.aoa;
import static com.projects.aoa.Print.*;
import java.util.*;
import java.io.*;
class Items {
String name, type;
int id;
int hp, mp, str, def;
boolean vacent;
static void getAllStats(Items[] e){
for(Items i : e){
getItemStats(i);
}
}
static void getItemStats(Items i){
i.getStats();
}
void getStats(){
try {
//System.out.println(System.getProperty("user.dir"));
print(this.name); //THIS shows up as null as well as those \/below\/
FileInputStream fstream = new FileInputStream(System.getProperty("user.dir")
+ "/src/com/projects/aoa/" + this.type + this.name + ".txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int counter = 0;
while ((line = br.readLine()) != null) {
if (line.length() == 0){
break;
}
switch (counter) {
case 0:
this.hp = Integer.parseInt(line);
counter++;
break;
case 1:
this.mp = Integer.parseInt(line);
counter++;
break;
case 2:
this.def = Integer.parseInt(line);
counter++;
break;
case 3:
this.str = Integer.parseInt(line);
counter++;
break;
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Game.java:
Helmet headBand = new Helmet("HeadBand");
Helmet bronzeHelmet = new Helmet("BronzeHelmet");
Items[] equips = {
headBand, bronzeHelmet
};
getAllStats(equips);
Equips.java:
package com.projects.aoa;
import static com.projects.aoa.Print.print;
import static com.projects.aoa.Print.println;
import java.io.*;
class Equips extends Items{
String name, type;
int hp, mp, str, def;
void printStats(){
println("[" + name + "]");
println("Type: " + type);
println("HP: " + hp);
println("MP: " + mp);
println("Def: " + def);
println("Str: " + str);
}
}
class Helmet extends Equips {
Helmet(String name){
this.name = name;
this.type = "h_";
}
}
You haven’t shown us your
Helmetclass, so it’s hard to say what’s going on – but my guess is that you’re redeclaring fields with the same name inHelmet. Those will hide the fields inItems, whereas you really just want to use the fields fromItems.So here’s a short but complete example which demonstrates what I think is going on:
I would recommend that:
Helmetwhich hide the ones inItemsItemandEquipmentinstead ofItemsHere’s a fixed version of the above code:
(Obviously you also need to think about what access to put on the properties etc, but that’s a separate matter.)