I want to pass the HashMap as a static member for each instance of the new class. However, every time I try to execute .get or.put in my HashMap, I get a NullPointerException. Help!
I do: public class EmailAccount { private static HashMap<String,Integer> name_list;and then name_list.put(last_name, occurences);Even name_list.containsKey(last_name);returns NullPointer.
public class EmailAccount { private static HashMap<String,Integer> name_list;
name_list.put(last_name, occurences);
name_list.containsKey(last_name);
This is related to an earlier question: Count string occurrences in Java
You need to create an instance.
private static Map<String, Integer> name_list = new HashMap<String, Integer>();
, "" . , name_map name_occurences? , , Java, .
name_map
name_occurences
,
private static HashMap<String, Integer> name_list = new HashMap<String, Integer>();
- , null.
, HashMap, , Java , HashMap , , HashMap, LinkedHashMap
, int, , private static int someNumber; NullPointerException, , , . Java ( int case, 0).
int
private static int someNumber;
You have not created an instance of the list. You declared this, but did not create an instance.
You created a field that may contain a HashMap, but you didn’t put anything into it
You need to put new HashMap<String, Integer>()in your box.
new HashMap<String, Integer>()
Source: https://habr.com/ru/post/1787342/More articles:How to create deb package with screenshot for Ubuntu? - ubuntuInteraction with a daemon in C ++ with sockets - c ++gridview paging - asp.netAugmented reality with large and complex markers - opencvReleasing loaded texture on webgl - webglDoes the iPhone display sliding photos / images (e.g. photo library and Facebook app)? - iphoneWriting a function using recursion functions or a higher order - haskellNumber of string occurrences in Java - javaHow can I draw a panel so that it does not blink? - c #SPAN tag ignored with JAWS read screen - htmlAll Articles