{"record":{"id":"ddc4536f8b6db138","repo":"JetBrains/intellij-community","slug":"cannot-bind-incompatible-types-cannot-assign","errorCode":null,"errorMessage":"Cannot bind: Incompatible types. Cannot assign ","messagePattern":"Cannot bind: Incompatible types\\. Cannot assign ","errorType":"validation","errorClass":"CodeGenerationException","httpStatus":null,"severity":"error","filePath":"java/compiler/forms-compiler/src/com/intellij/uiDesigner/compiler/AsmCodeGenerator.java","lineNumber":831,"sourceCode":"      if (binding == null) return;\n\n      validateFieldClass(binding, componentClass, component.getId());\n    }\n\n    private void validateFieldClass(String binding, InstrumentationClassFinder.PseudoClass componentClass, String componentId) throws CodeGenerationException {\n      if (!myFieldDescMap.containsKey(binding)) {\n        throw new CodeGenerationException(componentId, \"Cannot bind: field does not exist: \" + myClassToBind + \".\" + binding);\n      }\n\n      final Type fieldType = Type.getType(myFieldDescMap.get(binding));\n      if (fieldType.getSort() != Type.OBJECT) {\n        throw new CodeGenerationException(componentId, \"Cannot bind: field is of primitive type: \" + myClassToBind + \".\" + binding);\n      }\n\n      try {\n        final InstrumentationClassFinder.PseudoClass fieldClass = myFinder.loadClass(fieldType.getClassName());\n        if (!fieldClass.isAssignableFrom(componentClass)) {\n          throw new CodeGenerationException(componentId, \"Cannot bind: Incompatible types. Cannot assign \" + componentClass.getName().replace('/', '.') + \" to field \" + myClassToBind + \".\" + binding);\n        }\n      }\n      catch (ClassNotFoundException e) {\n        throw new CodeGenerationException(componentId, \"Class not found: \" + fieldType.getClassName());\n      }\n      catch (IOException e) {\n        throw new CodeGenerationException(componentId, e.getMessage(), e);\n      }\n    }\n\n    private void generateBorder(final LwContainer container, final GeneratorAdapter generator, final int componentLocal) {\n      final BorderType borderType = container.getBorderType();\n      final StringDescriptor borderTitle = container.getBorderTitle();\n      final String borderFactoryMethodName = borderType.getBorderFactoryMethodName();\n\n      final boolean borderNone = borderType.equals(BorderType.NONE);\n      if (!borderNone || borderTitle != null) {\n        // object to invoke setBorder","sourceCodeStart":813,"sourceCodeEnd":849,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/compiler/forms-compiler/src/com/intellij/uiDesigner/compiler/AsmCodeGenerator.java#L813-L849","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nprivate javax.swing.JTextField myField; // form component is javax.swing.JLabel\n\n// after\nprivate javax.swing.JLabel myField;","handlingStrategy":"validation","validationCode":"// Before compiling forms, check assignability for every binding\nClass<?> componentType = Class.forName(componentClassName);\nField field = classToBind.getDeclaredField(binding);\nif (!field.getType().isAssignableFrom(componentType)) {\n  throw new IllegalStateException(\"Field \" + binding + \" (\" + field.getType() + \") cannot hold \" + componentType);\n}","typeGuard":null,"tryCatchPattern":"try { generator.compile(); } catch (CodeGenerationException e) { if (e.getMessage().contains(\"Incompatible types\")) { /* retype field or rebind component using ids in message */ } else throw e; }","preventionTips":["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."],"tags":["gui-designer","forms-compiler","bytecode","type-mismatch","binding"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}