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

Javadoc should not suggest calling dialog.showAndWait().get() without checking isPresent()

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 8u40
    • 8u40
    • javafx
    • None

    Description

      As per the code here:

      http://hg.openjdk.java.net/openjfx/8u-dev/rt/file/4d239b9d4e31/modules/controls/src/main/java/javafx/scene/control/Dialog.java

      The javadoc for the Dialog.java currently contains an example like this:

      Option 1: The 'traditional' approach
      Optional<ButtonType> result = dialog.showAndWait();
        if (result.get() == ButtonType.OK) {
           formatSystem();
       }}

      and suggests that it is equivalent to the other two approaches listed which follow it.

      However, this is a poor example to show users -- it does not handle the possibility that the dialog has been cancelled by the user (clicking its close button, selecting a 'cancel' button, etc.) rather than via the user entering a value. If a programmer writes code like this, the user will get a NoSuchObjectException from Optional.get() if the user ever cancels the dialog without entering anything. Worse, since this is an unusual behavior, there is a good chance that the programmer will never get around to testing this particular code path.

      Please modify the javadoc for this class such that it does not suggest calling Optional.get() before first checking that Optional.isPresent() returns true (or at least if it suggests doing this that it also warns that the programmer must first have ensured that there is no way to cancel the dialog without entering anything).



      Attachments

        Activity

          People

            jgiles Jonathan Giles
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported: