I have a class ValueRepository class and want to use a logger in it. The following code always throws a NullPointerException.
public enum ValueRepository {
INSTANCE;
private static final Logger LOGGER = LoggerFactory.getLogger(ValueRepository.class.getName());
private Map<String, Set<String>> key2value;
private Map value2key;
ValueRepository() {
key2value = new HashMap();
value2key = new HashMap();
load("keyValue.txt");
}
public int size() {
return key2value.size();
}
public boolean hasKey(String key) {
return key2value.containsKey(key);
}
public boolean hasValue(String value) {
return value2key.containsKey(value);
}
public Set<String> getValues(String key) {
return key2value.get(key);
}
public String getKey(String value) {
return (String) value2key.get(value);
}
private void load(String filePath) {
BufferedReader br;
try {
br = IOUtils.fileReaderAsResource(filePath);
String line = null;
while ((line = br.readLine()) != null) {
LOGGER.info("test");
line = line.trim();
}
br.close();
} catch (IOException io) {
LOGGER.error("Can't load the file: " + filePath);
io.printStackTrace();
}
}
public static void main(String[] args) {
// do nothing
}
Shouldn't this work?
This question is very different from the link. It's specifically about why it doesn't work in logger in enum.
EDIT:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/congmi/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.6.2/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/congmi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at com.qe.repository.ValueRepository.load(ValueRepository.java:56)
at com.qe.repository.ValueRepository.<init>(ValueRepository.java:26)
at com.qe.repository.ValueRepository.<clinit>(ValueRepository.java:14)
Process finished with exit code 1