Bug 59211 NPE from RemoveFromClasspathAction when double clicking on a new jar in a ClasspathContainer
I have written an IClasspathContainer with an Initializer to sync the classpath with an external file. When the file I am monitoring changes the contents of the container is updated with the new jars, but when I double click on the jar to see the contents I get the following exception: java.lang.NullPointerException at org.eclipse.jdt.ui.actions.RemoveFromClasspathAction.canRemove(RemoveFromClasspathAction.java:134) at org.eclipse.jdt.ui.actions.RemoveFromClasspathAction.checkEnabled(RemoveFromClasspathAction.java:78) at org.eclipse.jdt.ui.actions.RemoveFromClasspathAction.selectionChanged(RemoveFromClasspathAction.java:71) at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchSelectionChanged(SelectionDispatchAction.java:200) at org.eclipse.jdt.ui.actions.SelectionDispatchAction.selectionChanged(SelectionDispatchAction.java:195) at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:159) at org.eclipse.core.internal.runtime.InternalPlatform.run(InternalPlatform.java(Inlined Compiled Code)) at org.eclipse.core.runtime.Platform.run(Platform.java(Compiled Code)) at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:157) at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:1310) at org.eclipse.jface.viewers.StructuredViewer.handleSelect(StructuredViewer.java:666) at org.eclipse.jface.viewers.StructuredViewer$4.widgetSelected(StructuredViewer.java:690) at org.eclipse.jface.util.OpenStrategy.fireSelectionEvent(OpenStrategy.java:178) at org.eclipse.jface.util.OpenStrategy.access$3(OpenStrategy.java:173) at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:309) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java(Compiled Code)) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java(Inlined Compiled Code)) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java(Compiled Code)) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java(Compiled Code)) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java(Compiled Code)) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1536) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:257) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:139) at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:90) at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:277) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:239) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:117) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:41) at java.lang.reflect.Method.invoke(Method.java:371) at org.eclipse.core.launcher.Main.basicRun(Main.java:267) at org.eclipse.core.launcher.Main.run(Main.java:692) at org.eclipse.core.launcher.Main.main(Main.java:676) As another point if I click the + button by the jar the + symbol disappears and I don't see any error. After restarting eclipse the jar's contents is correctly displayed.
org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/RemoveFromClasspathAction.java
} catch (InterruptedException e) {
			
		}
	}
	
	private static IPackageFragmentRoot[] getRootsToRemove(IStructuredSelection selection){
		List result= new ArrayList(selection.size()); 
		for (Iterator iter= selection.iterator(); iter.hasNext();) {
			Object element= iter.next();
			if (canRemove(element))
				result.add(element);
		}
		return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]);
	}	
	private static boolean canRemove(Object element){
		if (! (element instanceof IPackageFragmentRoot))
			return false;
		IPackageFragmentRoot root= (IPackageFragmentRoot)element;
		try {
			IClasspathEntry cpe= root.getRawClasspathEntry();
			if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
				return false; 
			return true;
		} catch (JavaModelException e) {
			JavaPlugin.log(e);
		}
		return false;
	}	
}