Reputation: 751
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...)
Upvotes: 0
Views: 574
Reputation: 718778
You say that:
dict
list in the created InteliCwDB
instance is empty.If the superclass constructor somehow didn't run, you would have an instance with a dict
that was null
.
If some other exception was thrown and not caught, then you wouldn't have a InteliCwDB
to 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.
dict
after the constructor has created and filled it. (You haven't declared dict
to be private ...).add
in 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.
Upvotes: 2
Reputation: 1107
You should add a catch (Exception e)
to your try-catch for the purposes of debugging. An exception that isn't an IOException
might be causing you problems. So, try like this:
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();
}catch(Exception e){
e.printStackTrace();
}
}
Upvotes: 0