Bug 129193 [JFace] Setting the sorter on AbstractTreeViewer before input can cause NPE
I have a scenario with a subclass of TreePathViewerSorter is set on a subclass of AbstractTreeViewer. AbstractTreeViewer.setSorter() calls refresh, which eventually ends up here: // AbstractTreeViewer: /* (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#getSortedChildren(java.lang.Object) */ protected Object[] getSortedChildren(Object parentElementOrTreePath) { Object[] result = getFilteredChildren(parentElementOrTreePath); ViewerSorter sorter = getSorter(); if (sorter instanceof TreePathViewerSorter) { TreePathViewerSorter tpvs = (TreePathViewerSorter) sorter; // be sure we're not modifying the original array from the model result = (Object[]) result.clone(); TreePath path = null; if (parentElementOrTreePath instanceof TreePath) { path = (TreePath) parentElementOrTreePath; } else { (1) Object parent = parentElementOrTreePath; (2) Widget w = internalGetWidgetToSelect(parent); if (w != null) { path = internalGetSorterParentPath(w, sorter); } if (path == null) path = TreePath.EMPTY.createChildPath(parentElementOrTreePath); } tpvs.sort(this, path, result); } else if (sorter != null) { // be sure we're not modifying the original array from the model result = (Object[]) result.clone(); sorter.sort(this, result); } return result; } At line (1) parent is null and at line (2) it eventually gets down into findItems(): // StructuredViewer: protected final Widget[] findItems(Object element) { Widget result = doFindInputItem(element); if (result != null) return new Widget[] { result }; // if we have an element map use it, otherwise search for the item. if (usingElementMap()) { (3) Object widgetOrWidgets = elementMap.get(element); if (widgetOrWidgets==null) { return NO_WIDGETS; } else if (widgetOrWidgets instanceof Widget) { return new Widget[] {(Widget) widgetOrWidgets}; } else { return (Widget[])widgetOrWidgets; } } result = doFindItem(element); return result == null ? NO_WIDGETS : new Widget[] { result }; } Here at (3), element is null, and the HashMap throws an NPE when trying to call key.hashCode() (since key is null).
bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java
}
		return false;
	}
	/**
     * Returns the index where the item should be inserted. It uses sorter to
     * determine the correct position, if sorter is not assigned, returns the
     * index of the element after the last.
     * 
     * @param items the items to search
     * @param sorter The sorter to use.
     * @param lastInsertion
     *            the start index to start search for position from this allows
     *            optimising search for multiple elements that are sorted
     *            themself.
     * @param element
     *            element to find position for.
     * @param parentPath the tree path for the element's parent or <code>null</code>
     * if the element is a root element or the sorter is not a {@link TreePathViewerSorter}
     * @return the index to use when inserting the element.
     * 
     */
	
	private int insertionPosition(Item[] items,  ViewerSorter sorter, int lastInsertion, Object element, TreePath parentPath) {
		
		int size = items.length;
		if (sorter == null) {
			return size;
		}
	    int min = lastInsertion, max = size - 1;
		while (min <= max) {