agrison / jtoml

TOML for Java
MIT License
45 stars 13 forks source link

Bug: getAs(Stringkey,Class<?>) returns NullPointerException for missing key, which is not helpful #21

Closed rwperrott closed 3 years ago

rwperrott commented 4 years ago
Caused by: java.lang.NullPointerException
    at me.grison.jtoml.impl.Toml.get(Toml.java:184)
    at me.grison.jtoml.impl.Toml.get(Toml.java:262)
    at me.grison.jtoml.impl.Toml.getAs(Toml.java:241)

Exceptions should not be used for flow control, so if the key is missing, null should be returned. I'll have to see if I can detect the absence of just the key, as a workaround for this bug, until it's fixed.

rwperrott commented 4 years ago
null == toml.getMap(key) ? null : toml.getAs(key, cls)

seems to do the trick.

I've also noticed that properties in an object values, with provided default values are overwritten by getAs, which seems a bit off, because you can't get automatic default values... It'd be better to get the default property value using getOrDefault, with that value as the default, then set the property to the resulting value.

rwperrott commented 3 years ago

Lost interest.