JetBrains/intellij-community · error · IncorrectOperationException
Cannot bind to default package: {0}
Error message
Cannot bind to default package: {0} What it means
When a Java code reference is re-bound to a package (e.g. after refactoring moves a class to another package), bindToPackage must construct a new reference text from the package's qualified name. The default (unnamed) package has an empty qualified name, and there is no way to write a reference to it, so it throws IncorrectOperationException("Cannot bind to default package: "+aPackage).
Source
Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java:790
default:
throw new IllegalArgumentException("Unexpected kind: " + kind);
}
ASTNode qualifier = findChildByRole(ChildRole.QUALIFIER);
if (qualifier == null) return false;
IElementType qualifierElementType = qualifier.getElementType();
LOG.assertTrue(qualifierElementType == JavaElementType.JAVA_CODE_REFERENCE, qualifierElementType);
PsiElement refElement = SourceTreeToPsiMap.<PsiJavaCodeReferenceElement>treeToPsiNotNull(qualifier).resolve();
if (refElement instanceof PsiPackage) return true;
return SourceTreeToPsiMap.<PsiJavaCodeReferenceElementImpl>treeToPsiNotNull(qualifier).isFullyQualified(containingFile);
}
private @NotNull PsiElement bindToPackage(@NotNull PsiPackage aPackage) throws IncorrectOperationException {
String qName = aPackage.getQualifiedName();
if (qName.isEmpty()) {
throw new IncorrectOperationException("Cannot bind to default package: "+aPackage);
}
PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(getProject()).getParserFacade();
PsiJavaCodeReferenceElement ref = parserFacade.createReferenceFromText(qName, getParent());
getTreeParent().replaceChildInternal(this, (TreeElement)ref.getNode());
return ref;
}
@Override
public boolean isReferenceTo(@NotNull PsiElement element) {
PsiFile containingFile = getContainingFile();
Kind kind = getKindEnum(containingFile);
switch (kind) {
case CLASS_NAME_KIND:
case CLASS_IN_QUALIFIED_NEW_KIND:
if (!(element instanceof PsiClass)) return false;
break;
case CLASS_FQ_NAME_KIND: {View on GitHub (pinned to be881553f2)
Solutions
- Avoid moving classes into the default package; keep at least one package level in the target.
- Before binding, check aPackage.getQualifiedName().isEmpty() and instead rewrite the reference to the simple class name (unqualified) plus fix imports.
- Use the standard Move refactoring which handles default-package targets by adjusting references rather than binding to a package qualifier.
Example fix
// before
ref.bindToElement(defaultPackage); // PsiPackage with qName "" -> throws
// after
if (element instanceof PsiPackage && ((PsiPackage)element).getQualifiedName().isEmpty()) {
// rebind to the class itself, not the default package
ref.bindToElement(targetClass);
} else {
ref.bindToElement(element);
} Defensive patterns
Strategy: validation
Validate before calling
if (element instanceof PsiPackage && ((PsiPackage)element).getQualifiedName().isEmpty()) {
// rebind to class or unqualify reference instead
} Type guard
static boolean isDefaultPackage(PsiElement e) { return e instanceof PsiPackage && ((PsiPackage)e).getQualifiedName().isEmpty(); } Try / catch
try { ref.bindToElement(pkg); } catch (IncorrectOperationException e) { /* rewrite reference text manually */ } Prevention
- Never move classes into the default package in refactored codebases.
- Check qualifiedName emptiness before binding references to packages.
When it happens
Trigger: bindToElement(element) where element resolves to a PsiPackage with empty qualified name — i.e. the default package — e.g. moving/importing a class so its reference qualifier would become the unnamed package.
Common situations: Move class refactoring that moves a class into the default package while qualified references in other files point at its old package; code that programmatically rebinds imports to package roots; projects mixing default-package and packaged classes.
Related errors
- Can't bind to non-labeled statement
- Invalid operation
- Unsupported operation
- File not in
- File name must end with .
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/f54a5cf9220c2797.
Report an issue: GitHub.