bevoy
bevoy

Reputation: 751

super() not working in subclass' constructor

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

Answers (2)

Stephen C
Stephen C

Reputation: 718778

You say that:

  1. There are no exceptions / stack traces.
  2. The 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.

  • You are describing the symptoms inaccurately.
  • The code you are actually running is substantially different to what you are showing us; e.g. there is some other code that is leaping in and emptying the dict after the constructor has created and filled it. (You haven't declared dict to be private ...).
    [ UPDATE - this was the explanation: see the OP's comments. He had overridden add in the subclass, and that's where the bug was. ]
  • The code you are running doesn't match your source code; i.e. you are not rebuilding / redeploying properly.

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

dshapiro
dshapiro

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

Related Questions