Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2861

Instantiating a class inside a script invoked by the GroovyScriptEngine causes a NullPointerException

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.5.4, 1.5.5, 1.5.6
    • Fix Version/s: 1.5.7, 1.6-beta-2
    • Component/s: None
    • Labels:
      None
    • Flags:
      Patch

      Description

      The following code attempts to instantiate an object dynamically inside a groovy script. It currently fails with a NullPointerException:

      String[] roots = new String[] { "some/path" }; 
      GroovyScriptEngine gse = new GroovyScriptEngine(roots); 
      binding.setVariable("instantiateMe", "com.package.TheClass"); 
       gse.run("hello.groovy", binding); 
      

      *******Now in hello.groovy script

      def clazz = getClass().getClassLoader().loadClass(instantiateMe, true)  //<-- NullPointerException occurs here.
      def h = clazz.newInstance() 
      

      The fix is to change this code around line 101 in the GroovyScriptEngine:

               currentCacheEntry.dependencies.put( 
                         dependentScriptConn.getURL(), 
                              new Long(dependentScriptConn.getLastModified())); 
      

      to this:

                if(currentCacheEntry != null) 
                     currentCacheEntry.dependencies.put( 
                           dependentScriptConn.getURL(), 
                                new Long(dependentScriptConn.getLastModified())); 
      

      This sort of dynamic instantiation also fails in 1.6-beta-1, but I haven't looked at the code there. I expect the same fix will apply.

      Note: I'll be glad to apply the fix myself. I'm very interested in having this work, because I have lots of code that depends on it.

        Attachments

          Activity

            People

            • Assignee:
              guillaume Guillaume Sauthier
              Reporter:
              ctassoni Charles Tassoni
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: