Java Another HashMap<Object, ArrayList<String>> null pointer exception -


this question has answer here:

ok, rewrote class,i rushed code not clean yet anyway comipiles, , running mainmethod problem still there.

   import java.util.*;      public class myclass {              public random myrandom;             public hashmap<string, arraylist<string>> mymap;             public arraylist<string> ar;             public arraylist<string> nexts;         //     arraylist<string> follows;             public myclass(){                 myrandom = new random();                    }               public void setrandom(int seed){                 myrandom = new random(seed);              }             public hashmap<string, arraylist<string>> buildhashmap(){               hashmap<string, arraylist<string>> mymap = new hashmap<string, arraylist<string>>();               arraylist<string> ar = new arraylist<string>();               ar.add("2");               ar.add("2");              string test = "test";              string anothertest = "anothertest";               mymap.put(test, ar);               mymap.put(anothertest, ar);                return mymap;               }             public arraylist<string> arraylistgetter(string st){                 system.out.println("++++++++++++++++++++++++++++++++++");                   system.out.println(mymap.size());                 arraylist ar = mymap.get(st);              return ar;           }              public void mainmethod(){                 hashmap<string, arraylist<string>> mymap = new hashmap<string, arraylist<string>>();                 mymap = buildhashmap();                 system.out.println("\n\nnumber of keys found: " + mymap.size());                  (string st : mymap.keyset()){                 system.out.println(st + ": ");                 arraylist<string> al = mymap.get(st);                      system.out.println(al.size());                       }                  stringbuilder sb = new stringbuilder();                 string test = "test";                 system.out.println(mymap.get(test));                  system.out.println(sb);                   system.out.println(mymap.get(test).size());                 system.out.println(mymap.get(test).size());         //         arraylist<string> follows = getfollows(key);                 system.out.println("something");                int  index = myrandom.nextint(mymap.get(test).size());                 system.out.println(index);         //         index = myrandom.nextint(follows.size());                 string next = mymap.get("test").get(index);                 sb.append(next);                 system.out.println(sb);                   system.out.println(mymap.get("test").getclass());                 arraylist<string> follows = new arraylist<string>();<------new empty arraylist                 system.out.println(follows.size() + " **********");                 arraylist<string> nexts = new arraylist<string>();                 nexts = arraylistgetter ("test");                 }             } 

am missing obvious? in advance time , patience.

you're shadowing mymap variable re-declaring in method , leaving field null.

import java.util.*;  public class myclass2 {     public hashmap<string, arraylist<string>> mymap;  // guy null      // arraylist<string> follows;     public hashmap<string, arraylist<string>> buildhashmap() {         hashmap<string, arraylist<string>> mymap = new hashmap<string, arraylist<string>>();         return mymap;     }      public arraylist<string> arraylistgetter(string st) {         system.out.println("++++++++++++++++++++++++++++++++++");         system.out.println(mymap.size());         arraylist ar = mymap.get(st);         return ar;     }      public void mainmethod() {         // mymap local variable, , assigning         // leave class field null         hashmap<string, arraylist<string>> mymap = new hashmap<string, arraylist<string>>();         mymap = buildhashmap();  // field in class still null!!!         system.out.println("\n\nnumber of keys found: " + mymap.size());         (string st : mymap.keyset()) {             system.out.println(st + ": ");             arraylist<string> al = mymap.get(st);              system.out.println(al.size());          }          // throws npe         arraylist<string> somemap =  arraylistgetter("test");     } } 

meaning you're initializing hashmap you've declared local method. yes you've given same name, mymap, field in class, declaring local method, variable visible in method only, , field in class remains null.

solution: don't this, don't give local fields same name fields, , don't re-declare variables in local scope if mean assign field in class.


Comments