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
return false;
	}
	
	private boolean check(IResource ancestor, IResource descendent) {
		descendent= descendent.getParent();
		while(descendent != null) {
			if (ancestor.equals(descendent))
				return true;
			descendent= descendent.getParent();
		}
		return false;
	}
	
	private boolean checkIfDescendant(int flags) {
		return (flags & CHECK_IF_DESCENDANT) != 0;
	}
	
	private boolean checkIfAncestor(int flags) {
		return (flags & CHECK_IF_ANCESTOR) != 0;
	}
	
	private boolean checkIfChild(int flags) {
		return (flags & CHECK_IF_CHILD) != 0;
	}
	
	private boolean checkContext(int flags) {
		return (flags & CHECK_CONTEXT) != 0;
	}
}