RFR: 6616245: NullPointerException when using JFileChooser with a custom FileView [v2]
Alexey Ivanov
aivanov at openjdk.org
Mon Oct 3 11:12:36 UTC 2022
On Fri, 30 Sep 2022 10:57:37 GMT, Tejesh R <tr at openjdk.org> wrote:
>> When a custom `FileView` is used and folder traversal is restricted to a particular directory NPE occurs when user tries to traverse/select other folders except traversable folder. This is caused because when user selects folder other than traversable, the traversal is rejected and hence no file is selected as `currentDirectory` of `JFileChooser`. When user tries to access the restricted folder second time, previous selected file check is failing because of NPE since `getFileChooser().getCurrentDirectory();` is null. To fix the issue, NPE check is added.
>
> Tejesh R has updated the pull request incrementally with one additional commit since the last revision:
>
> Updated based on review comments
You say that the current directory in `JFileChooser` gets set to `null` because of forbidden navigation to a folder which is not traversable. You avoid the NPE by a null-check.
However, you should prevent the current directory to be set to `null`. If navigation is not allowed, the current directory should remain unchanged _instead of being reset to `null`_.
src/java.desktop/share/classes/javax/swing/plaf/metal/MetalFileChooserUI.java line 1271:
> 1269: File curDir = getFileChooser().getCurrentDirectory();
> 1270:
> 1271: if (curDir != null && !curDir.equals(f)) {
If `f` is always not null, you can you use `!f.equals(curDir)`.
test/jdk/javax/swing/JFileChooser/FileViewNPETest.java line 84:
> 82: String path = "";
> 83: if (Platform.isWindows()) {
> 84: path = "C:" + File.separator + "temp";
It's rare but possible that Windows is installed not on `C:` drive.
test/jdk/javax/swing/JFileChooser/FileViewNPETest.java line 114:
> 112: } else {
> 113: return false;
> 114: }
I can be simplified to:
Suggestion:
return ((filePath != null) && (filePath.isDirectory())
&& filePath.getAbsolutePath().startsWith(basePath));
I'm not insisting though.
-------------
PR: https://git.openjdk.org/jdk/pull/10485
More information about the client-libs-dev
mailing list