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

NPE executing Collection.toArray( new String[ foo.size() ] )

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-5
    • 1.0-JSR-6
    • None
    • None
    • Sun j2se 1.4.2_04 on Microsoft Windows XP Pro and BEA WebLogic Server 8.1SP3

    Description

      The following script blows up with a NPE:

      def foo = [ 'aa', 'bb', 'cc' ]
      def goo = foo.toArray( new String[ foo.size() ] )

      Here is a stack trace:

      java.lang.NullPointerException
      at java.util.ArrayList.toArray(ArrayList.java:301)
      at gjdk.java.util.ArrayList_GroovyReflector.invoke(Unknown Source)
      at groovy.lang.MetaMethod.invoke(MetaMethod.java:111)
      at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassH
      elper.java:636)
      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:345)
      at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:144)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
      java:104)
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod(Script
      BytecodeAdapter.java:85)
      at Script1.run(Script1.groovy:2)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:521)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:496)

      This NPE does not show up with Groovy 1.0-jsr-04.

      It appears to be a regression in 1.0-jsr-05 for sure.

      Here is the junit test.
      import groovy.lang.Binding;
      import groovy.lang.GroovyShell;

      public void testGroovyArray()

      { Binding binding = new Binding(); GroovyShell shell = new GroovyShell( binding ); binding.setVariable( "goo", new String[0] ); StringBuffer script = new StringBuffer(); script.append( "def foo = [ 'aa', 'bb', 'cc' ]\n" ); script.append( "def goo = foo.toArray( new String[ foo.size() ] )\n" ); Object value = shell.evaluate( script.toString() ); assertTrue( value instanceof String[] ); assertTrue( ((String[])value).length == 3 ); }

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            ben@jetpen.com Ben Eng
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: