hashmap - How to remove the null pointer exception in java? -


this question has answer here:

i have file values like:

a,9,1

b,2,4

c,2,4

till *,2,0 , code:

    public static final string letterfileloc = "filelocation";     public static map<character, integer> lettervaluemap;     public static map<character, integer> lettercountmap;     public static void constructlettermaps() throws filenotfoundexception{     file pointvals = new file(letterfileloc);     scanner pts = new scanner(pointvals);     while (pts.hasnext()){         string [] parts = pts.next().split(",");         char alpha = (parts[0]).charat(0);         int counts = integer.parseint(parts[1]);         int values = integer.parseint(parts[2]);         lettervaluemap.put(alpha, values);         lettercountmap.put(alpha, counts);       } 

and keep getting null pointer expception when i'm putting values. don't understand why. please explain?

you need initialize lettervaluemap , lettercountmap this:

lettervaluemap = new hashmap<character, integer>(); lettercountmap = new hashmap<character, integer>(); 

put code anywhere in class run before 2 maps used.

you can initialize them when declared, this:

public static map<character, integer> lettervaluemap = new hashmap<character, integer>(); public static map<character, integer> lettercountmap = new hashmap<character, integer>(); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -