I have the following Java class:
public class CWTokenMap {
public static ConcurrentMap<String, String> allTokens
= new ConcurrentHashMap<String, String>();
public static void putTokenData(final String token, final String user) {
allTokens.put(token, user);
}
}
When I run my application, I try to access the putTokenData
method from another class and I get a NullPointerException
for the line allTokens.put(...)
. Why do I get this exception?
It was my understanding that the static ConcurrentMap
should be initialized by the time we access the putTokenData
method, but here it seems to not be the case. Are there cases where the static method will be called before initializing the static variable?