{"record":{"id":"5145cc177789e2fe","repo":"java-native-access/jna","slug":"no-field-named-in","errorCode":null,"errorMessage":"No field named  in ","messagePattern":"No field named  in ","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Union.java","lineNumber":108,"sourceCode":"            }\n        }\n        throw new IllegalArgumentException(\"No field of type \" + type + \" in \" + this);\n    }\n\n    /**\n     * Indicates which field will be used to write to native memory.\n     * @param fieldName desired field to use for the active union type\n     * @throws IllegalArgumentException if the name does not correspond to\n     * any declared union field.\n     */\n    public void setType(String fieldName) {\n        ensureAllocated();\n        StructField f = fields().get(fieldName);\n        if (f != null) {\n            activeField = f;\n        }\n        else {\n            throw new IllegalArgumentException(\"No field named \" + fieldName\n                                               + \" in \" + this);\n        }\n    }\n\n    /** Force a read of the given field from native memory.\n     * @return the new field value, after updating\n     * @throws IllegalArgumentException if no field exists with the given name\n     */\n    @Override\n    public Object readField(String fieldName) {\n        ensureAllocated();\n        setType(fieldName);\n        return super.readField(fieldName);\n    }\n\n    /** Write the given field value to native memory.\n     * The given field will become the active one.\n     * @throws IllegalArgumentException if no field exists with the given name","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Union.java#L90-L126","documentation":"Union.setType(String) throws IllegalArgumentException when the given field name does not correspond to any field registered on the union. JNA looks the name up in its field map and refuses to activate a nonexistent member, keeping the union's native layout consistent.","triggerScenarios":"Calling union.setType(\"fieldName\") with a misspelled name, a name of a field not declared in the Union subclass, or a name that changed during refactoring.","commonSituations":"Typo in field name; renaming a union field without updating setType callers; dynamically building the name from data that doesn't match declarations.","solutions":["Correct the field name to exactly match the declared Java field in the Union subclass.","Enumerate fields via the union's field map or use setType(Class) with the field's type instead.","Use setTypedValue(value) to select the active field by the value's runtime type."],"exampleFix":"// before\nunion.setType(\"intVal\"); // field is actually named intValue\n// after\nunion.setType(\"intValue\");","handlingStrategy":"validation","validationCode":"// Java\nboolean hasFieldName(Union u, String name) {\n    try { u.getClass().getField(name); return true; } catch (NoSuchFieldException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try { union.setType(\"intValue\"); } catch (IllegalArgumentException e) { log.error(\"unknown union field\", e); }","preventionTips":["Reference field names via constants derived from the declaring class","Compile-time: use setType(Class) instead of string names where possible","Rename fields via IDE refactoring to update all string usages"],"tags":["jna","union","illegal-argument","field-name"],"backgroundTag":"resource-not-found","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}