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

num.times fails in specific situation

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-4
    • 1.0-beta-5
    • None
    • None
    • beta-4 with snapshot jar from may 7

    Description

      In the code below, using the times loop rather than the for loop causes a crash with the message:
      Exception in thread "main" java.lang.VerifyError: (class: Test$1, method: doCall signature: (Ljava/lang/Object;)Ljava/lang/Object Expecting to find object/array on stack
      at Test.begin(test.groovy:17)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:63)
      at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:996)
      at groovy.lang.MetaClass.invokeMethod(MetaClass.java:304)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      ...

      To eliminate the error, convert the comments to either use the for loop or to use the simpler version of btn.move().

      Note: this is quite an odd code example, but it came from a far more complex environment using the RePast simulation environment. I've taken the original and stubbed out all the code I could in a half hr! Let me know if you need more help with this!

      Here's the code:
      #!/usr/bin/env groovy

      import java.awt.Color
      import java.util.ArrayList

      public class Test {
      ArrayList agents
      int numAgents = 4
      int spaceSize = 50

      public void setup()

      { Random.createUniform() agents = new ArrayList() }

      public void begin() {
      // for (i in 1..numAgents) {
      numAgents.times

      { TinyAgent btn = new TinyAgent() // btn.move(0,0) btn.move( Random.uniform.nextIntFromTo(0, spaceSize), Random.uniform.nextIntFromTo(0, spaceSize)) btn.setColor( new Color(Random.uniform.nextIntFromTo(0, Integer.MAX_VALUE))) agents.add(btn) }

      println agents.size()
      }
      }
      public class TinyAgent {
      int x; int y; Color color
      public void move(int nextX, int nextY)

      { x = nextX y = nextY }

      }
      public class Random {
      static Random uniform
      static void createUniform()

      {uniform = new Random()}

      int nextIntFromTo (int from, int to)

      {(from + to / 2).intValue()}

      }

      Test t = new Test()
      t.setup(); t.begin()

      Attachments

        Activity

          People

            Unassigned Unassigned
            backspaces Owen Densmore
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: