Skip to content

bindingsEngine.containsKey throws Exception when key is not found #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mslinn opened this issue Dec 5, 2017 · 1 comment
Open

Comments

@mslinn
Copy link

mslinn commented Dec 5, 2017

If no variable called ten is defined, the following Scala code throws java.lang.NullPointerException:

new ScriptEngineManager()
  .getEngineByName("clojure")
  .getContext
  .getBindings(ScriptContext.ENGINE_SCOPE)
  .containsKey("ten")

Similar code works for scripting-scala. However, this bug can is also present for scripting-java

@ctrueden
Copy link
Member

ctrueden commented Dec 18, 2017

I was not able to duplicate the NullPointerException (a stack trace would be helpful here).

Actually, I did manage to produce a NullPointerException by running the above as Groovy, with a fully qualified javax.script.ScriptEngineManager, but the stack trace is:

java.lang.NullPointerException: Cannot get property 'getContext' on null object
	at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60)
	at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
	at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
	at Script25.run(Script25.groovy:1)

So I'm guessing the problem is not actually related to the containsKey call, which never occurs, but rather than use of getContext.

Here is a Groovy script which gets farther, but certainly still demonstrates some weirdness:

#@ ScriptService ss

clojure = ss.getLanguageByName("clojure")
println("Found Clojure language: " + clojure.getClass().getName())

bindings = clojure.getScriptEngine().getBindings(javax.script.ScriptContext.ENGINE_SCOPE)

// output all the keys
println("Bindings has " + bindings.keySet().size() + " key-pairs:");
for (key in bindings.keySet()) {
	println("\t" + key + " = " + bindings.get(key))
}

// check for specific containment
def contains(key) {
	println("contains " + key + "? " + bindings.containsKey(key))
}
contains("context")
contains("ui")
contains("asdf")

On my system, it produces:

Found Clojure language: org.scijava.plugins.scripting.clojure.ClojureScriptLanguage
Bindings has 0 key-pairs:
contains context? true
contains ui? true
contains asdf? true

Further digging needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants