Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 431
Carlos Avatar asked Mar 05 '26 08:03

Carlos


2 Answers

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.

like image 110
Adeel Ansari Avatar answered Mar 07 '26 20:03

Adeel Ansari


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.

like image 38
hvgotcodes Avatar answered Mar 07 '26 22:03

hvgotcodes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!