RFR: 6616245: NullPointerException when using JFileChooser with a custom FileView [v4]

Alexey Ivanov aivanov at openjdk.org
Mon Oct 10 11:32:00 UTC 2022


On Tue, 4 Oct 2022 08:06:24 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:
> 
>   Test case update

Changes requested by aivanov (Reviewer).

test/jdk/javax/swing/JFileChooser/FileViewNPETest.java line 81:

> 79:         passFailJFrame = new PassFailJFrame("Test Instructions", INSTRUCTIONS, 5L, 13, 40);
> 80:         jfc = new JFileChooser();
> 81:         String path = System.getProperty("user.home");

The test does not work for me on Windows. It starts with `user.home` as the current folder as expected; when select `C:`, it goes to Desktop which is the root of the Shell namespace.

I guess, you have to change it to `System.getProperty("user.home") + File.separator + "Documents"` as you and I discussed. It'll make Desktop unreachable.

On Linux, the test works correctly.

test/jdk/javax/swing/JFileChooser/FileViewNPETest.java line 97:

> 95: 
> 96: class CustomFileView extends FileView {
> 97:     private String basePath;

Suggestion:

    private final String basePath;

test/jdk/javax/swing/JFileChooser/FileViewNPETest.java line 105:

> 103:     public Boolean isTraversable(File filePath) {
> 104:         return ((filePath != null) && (filePath.isDirectory())) &&
> 105:                 filePath.getAbsolutePath().startsWith(basePath);

Suggestion:

        return ((filePath != null) && (filePath.isDirectory()))
                && filePath.getAbsolutePath().startsWith(basePath);

I prefer wrapping before the operator, it makes it clear that it's a continuation line. Java Coding Style suggests wrapping this way.

-------------

PR: https://git.openjdk.org/jdk/pull/10485



More information about the client-libs-dev mailing list