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

NullPointerException caused by interaction of for loop scope and SwingBuilder

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-4
    • 1.0-JSR-5
    • None
    • None
    •  Ubuntu Linux, 1.0-JSR-4, on JDK 1.5.0

    Description

      I get a NullPointerException with this simple program unless I change the name of the variable inside the second for loop. The example is:

      import groovy.swing.SwingBuilder

      final int N_BUTTONS = 1

      for (i in 0..<N_BUTTONS) {
      for (j in 0..<N_BUTTONS) {
      }
      }
      //println j

      // I get:
      // groovy simpleswing.groovy
      // Caught: java.lang.NullPointerException
      // at simpleswing$_run_closure1.doCall(simpleswing.groovy)
      // at simpleswing$_run_closure1.doCall(simpleswing.groovy)
      // at simpleswing.run(simpleswing.groovy:21)
      // at simpleswing.main(simpleswing.groovy)
      // If I uncomment the println j, then I get:
      // 29: The current scope does already contain a variable of the name j
      // @ line 29, column 7.
      // for (j in 1..N_BUTTONS) {

      // The problem goes away if I use any variable other than the inner
      // loop variable from above as the loop variable below.

      // I suspect the bug is related to the weird scope rules of for loop
      // variables in Groovy. I recommend this behavior be changed: it
      // leads to very confusing and frustrating situations for
      // newbies. Much better to have the variable scope only be the for
      // loop itself, unless it was declared prior to the for loop.

      swing = new SwingBuilder()
      gui = swing.frame() {
      panel() {
      for (j in 1..N_BUTTONS)

      { b = button(text: "foo") println "Creating button." }

      }
      }

      gui.pack();
      gui.show();

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            riedl John Riedl
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: