java.lang.NullPointerException
when executing cache.[private|shared].get("...", () => null)
#386
Labels
bug
Something isn't working
Expected Behavior
The following code should output
null
to the console.Expected output:
The documentation at https://www.openhab.org/addons/automation/jsscripting/#cache says
The documentation for JSCache at https://openhab.github.io/openhab-js/JSCache.html says:
From this documentation there is no reason to assume a
defaultSupplier
cannot returnnull
(e.g.() => null
).Current Behavior
A
defaultSupplier
function that returnsnull
causes scripts to fail unexpectedly with ajava.lang.NullPointerException
. This is the actual output:The source code at https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/shared/ValueCache.java#L61 says:
Note that the
supplier
documentation mentions that itreturns a non-null value
. Apparently the code is not able to handle a default supplier that returnsnull
and doing so triggers the exception.Possible Solution
supplier
function to returnnull
without triggering aNullPointerException
. I wasn't able to immediately track down the source to try and submit a patch.null
to work around this. That is assuming there is only one reason the code can throw this exception. This change would introduce a risk of returningnull
when there is another reason to throw this exception. I don't like taking such risks.null
to avoid breaking their code. This is sub-optimal because it would require others to work around this problem in the code and any oversight on the side of the user would unexpectedly breaks their code with this exception.Steps to Reproduce (for Bugs)
null
but ajava.lang.NullPointerException
error message is shown instead.Context
I created a function that uses cache to store values but initialize them to a value that could sometimes be
null
. Using adefaultSupplier
that returnsnull
when appropriate would be the simplest implementation for this. However, I found it broke the script. I need to work around this by using.exists(key)
and having two different code paths to either get the cached value or get an initial value. The fragility of thedefaultSupplier
negates it's value; I'd be better off avoiding it so I never have to worry about running into this issue again.Your Environment
(Tip: It would be nice if the bug report template contained instructions on where to find the version information, as I have no idea where to find the openhab-js version)
4.2.1
(Release Version)17.0.12
Linux/6.1.21-v8+ (aarch64)
(Raspbian bullseye)The text was updated successfully, but these errors were encountered: