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

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.5.4, 1.5.5, 1.5.6
    • 1.5.7, 1.6-beta-2
    • None
    • None
    • 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

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

            Dates

              Created:
              Updated:
              Resolved: