← 输入框|github.com|刷新
Skip to content

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

Open

Description

@mslinn

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

Activity

  1. ctrueden commented on Dec 18, 2017

    @ctrueden
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions