Carlos
Carlos

Reputation: 5455

Problem accessing HashMap data from another class

Am having a problem accessing the data in a HashMap. It was created in one class and is being called from another. See below;

Created

public class LoadDatabase {
    public Map virusDatabase = new HashMap();
    ...
    public void toHash(String v_Name, String signature) {
        virusDatabase.put(v_Name, signature);
    }
    ...
    public void printDatabase() {   // This method is displaying correct data, so is being stored.
        Iterator iterator = virusDatabase.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            System.out.println(key + " = " + virusDatabase.get(key));
        }
    }
    ...
}

Need Access

public class LCS {
    LoadDatabase lb = new LoadDatabase();
    Tokenizer T = new Tokenizer();
    ...
    public void buildDataLCS(String[] inTokens) {
        Iterator iterator = lb.virusDatabase.keySet().iterator();
        ...                
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            String v_sig = (String) lb.virusDatabase.get(key);
            System.out.println(v_sig);  //Example of problem, nothing printed
        ...
    }
    ...
}

Why is the problem happening? Could you point me in the right direction.

Upvotes: 3

Views: 1867

Answers (2)

Adeel Ansari
Adeel Ansari

Reputation: 39907

Either of the 2 issues,

  1. You are not putting anything there. As I can't see your invocation of toHash(String v_Name, String signature) method.

  2. You are using 2 different instances of LoadDatabase class, somehow. Try making LoadDatabase singleton.

Upvotes: 4

hvgotcodes
hvgotcodes

Reputation: 120268

Carlos

I suspect you are not putting what you think you are putting into the map, or the keys when you put data in are not the same as when you take values out. I would log/print the key/val you put in, and then log/print the key/val you try to get out.

Upvotes: 1

Related Questions