hashmap - How to remove the null pointer exception in java? -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
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
Post a Comment