JetBrains/intellij-community · error · IncorrectOperationException
Cannot bind to {element}
Error message
Cannot bind to {element} What it means
Thrown by PsiPackageReference.bindToElement() when the element passed to rebind the reference is not a PsiPackage. Package references (used in import/package statements and package name references) can only be rebound to package PSI elements; anything else (a class, a directory, a PsiClass packaging element) is rejected with IncorrectOperationException. The call happens during refactorings such as package move/rename that re-point references.
Source
Thrown at java/java-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/PsiPackageReference.java:74
}
@Override
public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) {
return doMultiResolve();
}
protected ResolveResult @NotNull [] doMultiResolve() {
Collection<PsiPackage> packages = new HashSet<>();
for (PsiPackage parentPackage : getContext()) {
packages.addAll(myReferenceSet.resolvePackageName(parentPackage, getValue()));
}
return PsiElementResolveResult.createResults(packages);
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
if (!(element instanceof PsiPackage)) {
throw new IncorrectOperationException("Cannot bind to " + element);
}
TextRange range = new TextRange(getReferenceSet().getReference(0).getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset());
String newName = ((PsiPackage)element).getQualifiedName();
return ElementManipulators.handleContentChange(getElement(), range, newName);
}
public PackageReferenceSet getReferenceSet() {
return myReferenceSet;
}
}View on GitHub (pinned to be881553f2)
Solutions
- Pass a PsiPackage to bindToElement: resolve the target package via JavaPsiFacade.getInstance(project).findPackage(qualifiedName) and check it is non-null first.
- If the target is really a class, rebind the underlying class reference, not the package reference.
- Guard with 'instanceof PsiPackage' before calling bindToElement and log/skip otherwise.
Example fix
// before
ref.bindToElement(targetClass); // PsiClass -> throws
// after
PsiPackage pkg = JavaPsiFacade.getInstance(project).findPackage("com.example.newpkg");
if (pkg != null) ref.bindToElement(pkg); Defensive patterns
Strategy: type-guard
Validate before calling
PsiPackage target = JavaPsiFacade.getInstance(project).findPackage(newFqn);
if (target == null) { /* package missing; create or abort */ } Type guard
static boolean isRebindablePackageTarget(@NotNull PsiElement e) {
return e instanceof PsiPackage && ((PsiPackage)e).getQualifiedName() != null;
} Try / catch
try { ref.bindToElement(target); } catch (IncorrectOperationException e) { /* target was not a PsiPackage; log and skip rebinding this reference */ } Prevention
- Always resolve the target to a PsiPackage via JavaPsiFacade.findPackage before bindToElement on package references.
- Check the reference type (PsiPackageReference vs PsiJavaCodeReferenceElement) before rebinding.
- In custom refactoring plugins, re-run resolution after each bind to catch shifted ranges.
When it happens
Trigger: Calling bindToElement() on a PsiPackageReference with a PsiClass, PsiJavaFile, PsiDirectory or PsiPackage with null qualified name instead of a PsiPackage; custom refactoring handlers wiring a wrong target element into a move/rename of packages.
Common situations: Plugin authors implementing custom move refactorings and passing resolved classes where packages are expected; code that assumes bindToElement accepts any named element; edge cases where a reference resolves ambiguously and the caller feeds the wrong resolve result back in.
Related errors
- Cannot create {0} - incorrect {1} template.
- Cannot create class-file
- error.package.set.position.parsing.error
- changeSignature.contract.converter.external.annotations
- changeSignature.contract.converter.inherited.annotation
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/0bd0041c68be54bb.
Report an issue: GitHub.