Maps with null values cause an unclear exception
See original GitHub issueWe have a rather complex application that is getting its configuration from all over the place.
We’re in the process of cleaning all of that up and OWNER is a big part of that effort. However, we do have to ease into this and so we have configuration coming from many different sources and we’re funneling that into OWNER.
Working on this I just stumbled upon a “problem” where one of the maps that I was passing into OWNER when creating a configuration interface via the ConfigFactory contained a null value.
This resulted in a very cryptic NullPointerException where I had no idea what was causing the problem. It took me quite a while to track down the problem.
I’ve created a unit test that demonstrates the problem in its simplest form and it’s at the bottom of this issue.
I’m wondering whether doing a sanity check to make sure that the maps passed into ConfigFactory.create() do not contain null values would be a good idea? If such a property is encountered, an IllegalArgumentException could be thrown with the key name in the message.
public class Issue184 {
public interface MyConfig extends Config {
@Key("null.value.key")
@DefaultValue("1")
Integer getNullValueKey();
}
private Issue184.MyConfig cfg;
@Before
public void before() {
Map<String,String> propsMapWithNullValue = new HashMap<String,String>();
propsMapWithNullValue.put("null.value.key", null);
Map<String,String> propsMapWithValidValue = new HashMap<String,String>();
propsMapWithValidValue.put("null.value.key", "42");
cfg = ConfigFactory.create(Issue184.MyConfig.class, propsMapWithValidValue, propsMapWithNullValue);
}
@Test
public void testInvalidValueArray() throws Exception {
cfg.getNullValueKey();
}
}
The above code will throw the following exception:
java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:459)
at java.util.Hashtable.putAll(Hashtable.java:523)
at org.aeonbits.owner.PropertiesManager.merge(PropertiesManager.java:349)
at org.aeonbits.owner.PropertiesManager.load(PropertiesManager.java:219)
at org.aeonbits.owner.PropertiesManager.load(PropertiesManager.java:207)
at org.aeonbits.owner.PropertiesInvocationHandler.<init>(PropertiesInvocationHandler.java:54)
at org.aeonbits.owner.DefaultFactory.create(DefaultFactory.java:46)
at org.aeonbits.owner.ConfigFactory.create(ConfigFactory.java:66)
at org.aeonbits.owner.issues.Issue184.before(Issue184.java:31)
...
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (4 by maintainers)

Top Related StackOverflow Question
I released today, I think that some documentation for this feature may help users. 👍
nevermind… figured it out.