New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FileChooser does not handle a the value returned when the user cancels file selection #178
Milestone
Comments
This was found to occur in SFX-2. The 8._ branch has not been tested. |
jpsacha
added a commit
that referenced
this issue
Jan 26, 2015
…d user cancelled selection an NPE was thrown. Also clatify in Scaladoc that null may be returned.
Fixed using option 1 at best aligns with conventions currently used in ScalaFX (return null is JavaFX documents returning null). |
Thanks for the quick fix! For reference purposes, SI-9113, the related language issue, now exists. |
RomanHargrave
added a commit
to RomanHargrave/CUEComposer
that referenced
this issue
Jan 28, 2015
adriaanm
pushed a commit
to adriaanm/scala
that referenced
this issue
Feb 13, 2015
`scala.collection.{JavaConversions, JavaConverters}` no longer wrap a `null` collection, but return `null` instead. Before, a wrapper would hold the original `null` collections, delaying the NPE until the first operation on the collection was called, with no way of knowing whether we were holding such a time bomb wrapper until the NPE was thrown. For example, this now holds: `(null : java.util.Iterator[_]).asScala == null`. Before, a wrapper would silently be created that would NPE on any further interaction. An example of this issue in the wild can be seen at scalafx/scalafx#178, and an example of code that generates such a wrapper [before the issue was fixed](https://github.com/scalafx/scalafx/blob/ad60d5faee687b71d3c0474510d 3f3dd081ea351/scalafx/src/main/scala/scalafx/stage/FileChooser.scala#L138).
adriaanm
pushed a commit
to adriaanm/scala
that referenced
this issue
Feb 13, 2015
`scala.collection.{JavaConversions, JavaConverters}` no longer wrap a `null` collection, but return `null` instead. Before, a wrapper would hold the original `null` collections, delaying the NPE until the first operation on the collection was called, with no way of knowing whether we were holding such a time bomb wrapper until the NPE was thrown. For example, this now holds: `(null : java.util.Iterator[_]).asScala == null`. Before, a wrapper would silently be created that would NPE on any further interaction. An example of this issue in the wild can be seen at scalafx/scalafx#178, and an example of code that generates such a wrapper [before the issue was fixed](https://github.com/scalafx/scalafx/blob/ad60d5faee687b71d3c0474510d 3f3dd081ea351/scalafx/src/main/scala/scalafx/stage/FileChooser.scala#L138).
adriaanm
pushed a commit
to adriaanm/scala
that referenced
this issue
Feb 19, 2015
`scala.collection.{JavaConversions, JavaConverters}` no longer wrap a `null` collection, but return `null` instead. Before, a wrapper would hold the original `null` collections, delaying the NPE until the first operation on the collection was called, with no way of knowing whether we were holding such a time bomb wrapper until the NPE was thrown. For example, this now holds: `(null : java.util.Iterator[_]).asScala == null`. Before, a wrapper would silently be created that would NPE on any further interaction. An example of this issue in the wild can be seen at scalafx/scalafx#178, and an example of code that generates such a wrapper [before the issue was fixed](https://github.com/scalafx/scalafx/blob/ad60d5faee687b71d3c0474510d 3f3dd081ea351/scalafx/src/main/scala/scalafx/stage/FileChooser.scala#L138).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FileChooser.showOpenMultipleDialog
is a bridge method of return typeSeq[File]
that callsjavafx.stage.FileChooser
, which has a return type ofjava.util.List[File]
. In the event that the user has selected 0 files (or the system toolkit returns null), the method will return null implicitly cast asjava.util.List[String]
. This will happen during runtime when the user is presented with a multiple file selection dialog and chooses theCancel
option (documentation forcom.sun.glass.ui.CommonDialogs.FileChooserResult
verifies that an empty list signifies cancellation). When said implicitly cast null value reaches the caller in scalafx, an implicit conversion is made fromjava.util.List[java.io.File]
toscala.collection.Seq[java.io.File]
. Because the implicit conversion process involves performing operations on the underlying collection, aNullPointerException
is thrown originating in thescala.collection
API, which travels back up to the caller ofscalafx.stage.FileChooser.showOpenMultipleDialog
. Due to the nature ofNullPointerException
, it makes it hard to programmatically determine whether or not this error was caused by the selection ofCancel
. This makes it exceptionally hard to develop a case to handle scenarios where you must perform an action after the potential cancellation has occurred, as the only way to do so is to place the file selection dialog call inside of a try/catch block with a blanket catch forNullPointerException
.In the interest of maintaining method signature, I would suggest one of the following solutions:
Seq[File]
Seq[File]
Solution one offers what a good solution, as it retains signature, but returns null as opposed to halting the thread. New implementers may use Option.apply(Any) to wrap the return value and deal with the problem in a sane manner.
Solution two offers also offers a good solution, as it retains signature, and returns an empty
Seq
of the expected type. This means that any functions called against the sequence, most likely iterators, will act upon no file as opposed to resulting in aNullPointerException
.I am aware that this issue is more of a fault of scala's
JavaConversions
bundle than scalafx, and I plan to report this to the scala bug tracker as well.This Gist demonstrates some scalafx-agnostic code that will generate a similar exception.
The text was updated successfully, but these errors were encountered: