简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM