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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.ui;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.ui.IContainmentAdapter;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.JavaCore;
public class JavaElementContainmentAdapter implements IContainmentAdapter {