JetBrains/intellij-community · error · CodeGenerationException
Cannot bind: Incompatible types. Cannot assign
Error message
Cannot bind: Incompatible types. Cannot assign
What it means
Thrown by AsmCodeGenerator.validateFieldClass when the class of a bound field loads successfully but is not assignable from the component's class (fieldClass.isAssignableFrom(componentClass) == false). The generated instrumentation code would assign the instantiated component to the field, so the compiler rejects the form at compile time instead of producing bytecode that fails at runtime with ClassCastException.
Source
Thrown at java/compiler/forms-compiler/src/com/intellij/uiDesigner/compiler/AsmCodeGenerator.java:831
if (binding == null) return;
validateFieldClass(binding, componentClass, component.getId());
}
private void validateFieldClass(String binding, InstrumentationClassFinder.PseudoClass componentClass, String componentId) throws CodeGenerationException {
if (!myFieldDescMap.containsKey(binding)) {
throw new CodeGenerationException(componentId, "Cannot bind: field does not exist: " + myClassToBind + "." + binding);
}
final Type fieldType = Type.getType(myFieldDescMap.get(binding));
if (fieldType.getSort() != Type.OBJECT) {
throw new CodeGenerationException(componentId, "Cannot bind: field is of primitive type: " + myClassToBind + "." + binding);
}
try {
final InstrumentationClassFinder.PseudoClass fieldClass = myFinder.loadClass(fieldType.getClassName());
if (!fieldClass.isAssignableFrom(componentClass)) {
throw new CodeGenerationException(componentId, "Cannot bind: Incompatible types. Cannot assign " + componentClass.getName().replace('/', '.') + " to field " + myClassToBind + "." + binding);
}
}
catch (ClassNotFoundException e) {
throw new CodeGenerationException(componentId, "Class not found: " + fieldType.getClassName());
}
catch (IOException e) {
throw new CodeGenerationException(componentId, e.getMessage(), e);
}
}
private void generateBorder(final LwContainer container, final GeneratorAdapter generator, final int componentLocal) {
final BorderType borderType = container.getBorderType();
final StringDescriptor borderTitle = container.getBorderTitle();
final String borderFactoryMethodName = borderType.getBorderFactoryMethodName();
final boolean borderNone = borderType.equals(BorderType.NONE);
if (!borderNone || borderTitle != null) {
// object to invoke setBorderView on GitHub (pinned to be881553f2)
Solutions
- Make the field type exactly the component's class or a supertype of it (usually the concrete swing class shown in the error message: 'Cannot assign X to field Y.Z').
- If the component class was swapped in the designer, update the Java field declaration to match the new component type.
- If two copies of a class are on the classpath (duplicate jars), remove the duplicate so both field type and component type resolve to the same class.
- Rebuild the project so instrumentation uses the current compiled classes.
Example fix
// before private javax.swing.JTextField myField; // form component is javax.swing.JLabel // after private javax.swing.JLabel myField;
Defensive patterns
Strategy: validation
Validate before calling
// Before compiling forms, check assignability for every binding
Class<?> componentType = Class.forName(componentClassName);
Field field = classToBind.getDeclaredField(binding);
if (!field.getType().isAssignableFrom(componentType)) {
throw new IllegalStateException("Field " + binding + " (" + field.getType() + ") cannot hold " + componentType);
} Try / catch
try { generator.compile(); } catch (CodeGenerationException e) { if (e.getMessage().contains("Incompatible types")) { /* retype field or rebind component using ids in message */ } else throw e; } Prevention
- When swapping a component in the designer, let the IDE update or re-create the bound field.
- Keep custom component hierarchies stable, or bind fields to a stable supertype (e.g. JComponent) where appropriate.
- Avoid duplicate jars providing the same component class.
When it happens
Trigger: A form component of class javax.swing.JLabel is bound to a field declared as 'JTextField myLabel', or a custom component class does not extend the field's declared type. myFinder.loadClass succeeds for the field type, but the PseudoClass assignability check fails and CodeGenerationException names both the component class and the field.
Common situations: Renaming or reparenting a component class after the form was created; swapping a component in the designer (e.g. JLabel -> JTextArea) without updating the field type; binding a JPanel to a field typed as a specific custom subclass; classloader differences where the 'same' class is loaded from two different jars.
Related errors
- Cannot bind: field is of primitive type:
- No binding on root component of nested form
- Error loading nested form:
- Class not found:
- Unsupported class version error:
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/ddc4536f8b6db138.
Report an issue: GitHub.