Superclass:
public class CwDB{
protected LinkedList<Entry> dict = null;
public CwDB(String filename){
this.dict = new LinkedList<Entry>();
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String w = null;
while((w = br.readLine()) != null ){
String c = br.readLine();
this.add(w,c); //adds new Entry to dict
}
br.close();
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void add(String word, String clue){
this.dict.add(new Entry(word,clue));
}
...
}
Subclass:
public class InteliCwDB extends CwDB {
public InteliCwDB(String filename){
super(filename);
}
}
Case 1:
CwDB db = new CwDB("src/cwdb.txt");
Case 2:
InteliCwDB idb = new InteliCwDB("src/cwdb.txt");
The problem is that case 1 works perfectly, but case 2 doesn’t at all.
Can you tell me what’s wrong?
(I don’t get any error/exception, just the idb’s list is empty, when db’s list has ober +1k elements…)
You say that:
dictlist in the createdInteliCwDBinstance is empty.If the superclass constructor somehow didn’t run, you would have an instance with a
dictthat wasnull.If some other exception was thrown and not caught, then you wouldn’t have a
InteliCwDBto examine.So on the face of it, this can only mean that the constructor is running, but the file it reads is empty. In other words you have two different “src/cwdb.txt” files … in different directories.
Other explanations involve questioning your evidence; e.g.
dictafter the constructor has created and filled it. (You haven’t declareddictto be private …).[ UPDATE – this was the explanation: see the OP’s comments. He had overridden
addin the subclass, and that’s where the bug was. ]My advice would be to recheck your processes and your evidence. And if that doesn’t help, then run the application using a debugger, set a breakpoint on the constructor, and single step it so that you can work out what is really happening.