Project: JDT
Bug 38971 unhandled NullPointerException in JavaElementContainmentAdapter
Reported: 2003-06-16 15:23:25 | Status: resolved fixed

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

Files changed in fix:
org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/JavaElementContainmentAdapter.java