java - How to update values in HashMap with LinkedList? -


this hashmap:

public static hashmap<string, linkedlist<linkedlist<string>>> partitionmap; partitionmap = new hashmap<string, linkedlist<linkedlist<string>>>(); 

my program has initial step of initialization keys added, without values. after that, need retrieve key , add value. problem got null pointer exception if initizalize linkedlist.

init step:

linkedlist<linkedlist<string>> ll = new linkedlist<linkedlist<string>>(); partitionmap.put(key, ll); 

after that:

linkedlist<linkedlist<string>> l = partitionmap.get(key); l.add(partition);  //crash, null pointer exception partitionmap.put(key, l); 

the problem related linkedlist , initialization. there way avoid problem?

edit: full code.

   //this function called n time fill partitionmap keys    public void init(dlrparser.signaturecontext ctx) {     linkedlist<linkedlist<string>> l = new linkedlist<linkedlist<string>>();     partitionmap.put(ctx.gettext(), l); }    //after that, function called fill partitionmap values public void processing(dlrparser.multiprojectioncontext ctx) {       linkedlist<string> partition = new linkedlist<string>();     (terminalnode terminalnode : ctx.u()) {         partition.add(terminalnode.gettext());     }     collections.reverse(partition);      //iteration on hashmap same keys, if have match     //then add values partitionmap     for(entry<string, linkedlist<string>> entry : tablemap.entryset())     {         string key = entry.getkey();         linkedlist<string> attributes = entry.getvalue();         if(attributes.containsall(partition)) //match         {             //retrieve linkedlist of linkedlist value             linkedlist<linkedlist<string>> l = partitionmap.get(key);             l.add(partition); // crash - nullpointer exception             partitionmap.put(key, l); //add -             system.out.println(l.tostring());         }     } } 

try putifabsent method added in java8 initializing list default values

    hashmap<string, list<list<string>>> partitionmap = new hashmap<string, list<list<string>>>();     partitionmap.putifabsent("a", new linkedlist<>(new linkedlist<>()));     partitionmap.get("a").add(arrays.aslist("b")); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -