Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6631269

DefaultMutableTreeNode postorder enumeration does not throw exception when empty

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      A postorder enumeration returned by DefaultMutableTreeNode will return null from nextElement() instead of throwing an exception after hasMoreElements() returns false.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a tree of one or more DefaultMutableTreeNode objects, create a postorder enumeration of the elements by calling postorderEnumeration() (or depthFirstEnumeration() , which does the same thing). Exhaust the enumeration by calling nextElement(). Then call nextElement() again.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After hasMoreElements() returns false, calling nextElement() should throw a NoSuchElementException exception.
      ACTUAL -
      nextElement() returns null if there are no more elements.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Enumeration;
      import java.util.NoSuchElementException;
      import javax.swing.tree.DefaultMutableTreeNode;

      public class TreeTest {

          public TreeTest() {
              DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
              root.add(new DefaultMutableTreeNode("child"));
              Enumeration enum_ = root.depthFirstEnumeration();
              while (enum_.hasMoreElements()) {
                  System.out.println(enum_.nextElement().toString());
              }
              try {
                  Object obj = enum_.nextElement();
              } catch (NoSuchElementException e) {
                  System.out.println("Exception thrown");
                  e.printStackTrace();
              }
              System.out.println("Exception NOT thrown!!!!!");
          }
          
          public static void main(String[] args) {
              TreeTest app = new TreeTest();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Avoid writing code that relies on nextElement() throwing an exception.

      Attachments

        Activity

          People

            Unassigned Unassigned
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: