JDK-7080203 : JTree.getSelectionPaths() now returns empty array instead of null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_vista,windows_7
  • CPU: x86
  • Submitted: 2011-08-17
  • Updated: 2013-09-26
  • Resolved: 2011-11-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u4Fixed 8 b14Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6002]

A DESCRIPTION OF THE PROBLEM :
Documentation specifies that JTree.getSelectionPaths() returns null if there is no selection. This was always the case until 1.7.0, where an empty array is returned instead.

REGRESSION.  Last worked in version 6u26

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the supplied Applet

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
OK should appear in the Applet
ACTUAL -
FAIL!! appears in the Applet

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

public class GetSelectionPathsBug extends JApplet {

    public void init() {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JTree t = new JTree();
                    TreePath[] selPaths = t.getSelectionPaths();
                    JLabel lab = new JLabel(selPaths == null ? "OK" : "FAIL!!");
                    getContentPane().add(lab);
                }});
    }
}

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

CUSTOMER SUBMITTED WORKAROUND :
Use a subclass with:

@Override
TreePath[] getSelectionPaths() {
    TreePath[] res = super.getSelectionPaths();
    return res != null && res.length == 0 ? null : res;
}

or add explicit test for empty array wherever a null check is currently carried out

Comments
Verified!
22-05-2013

EVALUATION Because of 1. TreeSelectionModel#getSelectionPaths returns null or an empty array if nothing is currently selected 2. JTree#getSelectionPaths specifies that it returns null if nothing is currently selected we shuold add empty array checking into the JTree#getSelectionPaths method and returns null instead of empty arrays
05-11-2011