Bug 38971 unhandled NullPointerException in JavaElementContainmentAdapter
The following code is copied from method "contains(Object, Object, int)", and fails if "element" is an IJavaElement not contained in a "workingSetElement" of type IJavaElement.PACKAGE_FRAGMENT. I believe all that is needed is to set "resource" using jElement.getResource(): ... IResource resource= null; IJavaElement jElement= null; if (element instanceof IJavaElement) { jElement= (IJavaElement)element; /***resource should be set here ***/ } else { if (element instanceof IAdaptable) { resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); if (resource != null) { if (fJavaModel.contains(resource)) { jElement= JavaCore.create(resource); if (jElement != null && !jElement.exists()) jElement= null; } } } } if (jElement != null) { if (contains(workingSetJavaElement, jElement, flags)) return true; if (workingSetJavaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT && //***THE FOLLOWING THROWS AN NPE, "resource" IS NULL*** resource.getType() == IResource.FOLDER && checkIfDescendant(flags)) return isChild(workingSetJavaElement, resource); Simply adding the check (resource != null) would avoid the NPE
org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/JavaElementContainmentAdapter.java
private IJavaModel fJavaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
	public boolean contains(Object workingSetElement, Object element, int flags) {
		if (!(workingSetElement instanceof IJavaElement) || element == null)
			return false;
						
		IJavaElement workingSetJavaElement= (IJavaElement)workingSetElement;
		IResource resource= null;		
		IJavaElement jElement= null;
		if (element instanceof IJavaElement) {
			jElement= (IJavaElement)element;	
		} else {
			if (element instanceof IAdaptable) {
				resource= (IResource)((IAdaptable)element).getAdapter(IResource.class);
				if (resource != null) {
					if (fJavaModel.contains(resource)) {
						jElement= JavaCore.create(resource);
						if (jElement != null && !jElement.exists())
							jElement= null;		
					}
				}
			}
		}
		
		if (jElement != null) {