I got NullPointerException at myset.contains(obj) and stacktrace like this:
java.lang.NullPointerException: null
at java.util.HashSet.contains(HashSet.java:203) ~[?:1.8.0_131]
I looked in to source code of HashSet,
private transient HashMap<E,Object> map;
...
202 public boolean contains(Object o) {
203 return map.containsKey(o);
204 }
so seems map is null, and my HashSet object is not. But every init method of HashSet creates a HashMap Object and assigns to map, like
public HashSet() {
map = new HashMap<>();
}
So my question is, why can map become null in line 203?
This happens sometime in our web server, myset is used by multiple threads. I understand there could be inconsistent issue on a non-threadsafe HashSet, but I don't get why it became null.
Thanks in advance.
Post my code here:
Set<String> tags = data.getTags();
if (tags.contains(tmp.toString())) {
return true;
}
class definition of data, which is accessed by multiple threads:
class Data
private Set<String> tags;
public Set<String> getTags() {
if (tags == null) {
tags = new HashSet<String>();
// add something to tags
}
return tags;
}