United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7080203 JTree.getSelectionPaths() now returns empty array instead of null
JDK-7080203 : JTree.getSelectionPaths() now returns empty array instead of null

Details
Type:
Bug
Submit Date:
2011-08-17
Status:
Closed
Updated Date:
2013-09-26
Project Name:
JDK
Resolved Date:
2011-11-16
Component:
client-libs
OS:
windows_vista,windows_7
Sub-Component:
javax.swing
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:
Duplicate:
Relates:

Sub Tasks

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
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
                                     
2011-11-05
Verified!
                                     
2013-05-22



Hardware and Software, Engineered to Work Together