question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maps with null values cause an unclear exception

See original GitHub issue

We 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:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lviggianocommented, Mar 1, 2018

I released today, I think that some documentation for this feature may help users. 👍

0reactions
StFScommented, Mar 1, 2018

nevermind… figured it out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Map.of not allow null keys and values?
If it returns null , it is unclear whether the key is missing or the value was null . Generally speaking, collections that...
Read more >
Unable to handle Attempt to de-reference a null object with map
You're error is because accWithCaseSalesDiv.get(i.Id) returns null and you are attempting to dereference it .Id.
Read more >
[JavaSpecialists 303] - Null Keys and Values in Maps
Some Map implementations allow null keys and values. This leads to funky behaviour when calling putIfAbsent() and the compute functions.
Read more >
SQL error messages and exceptions - Oracle Help Center
01003, Null values were eliminated from the argument of a column function. ... This may be caused by catching a transaction severity error...
Read more >
Understanding null safety - Dart
So you can use nullable types as map keys, store them in sets, ... This program compiles but then throws an exception at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found